Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findByID JPA not working #7

Open
ayushbinnie opened this issue Jan 8, 2021 · 0 comments
Open

findByID JPA not working #7

ayushbinnie opened this issue Jan 8, 2021 · 0 comments

Comments

@ayushbinnie
Copy link

package com.ayush.jpa.JpaDemo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class JpaDemoApplication implements CommandLineRunner {

private Logger logger = LoggerFactory.getLogger(JpaDemoApplication.class);

@Autowired
private PersonRepository repo;

public static void main(String[] args) {
	SpringApplication.run(JpaDemoApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
	// TODO Auto-generated method stub

	logger.info("Person findById----", repo.findById(10002));

}

}

package com.ayush.jpa.JpaDemo;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@entity
public class Person {

@Id
@GeneratedValue
private int id;

private String name;
private String location;
private Date birthDate;

public Person() {

}

public Person(int id, String name, String location, Date birthDate) {
	super();
	this.id = id;
	this.name = name;
	this.location = location;
	this.birthDate = birthDate;
}

public Person(String name, String location, Date birthDate) {
	super();
	this.name = name;
	this.location = location;
	this.birthDate = birthDate;
}

public int getId() {
	return id;
}

public void setId(int id) {
	this.id = id;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public String getLocation() {
	return location;
}

public void setLocation(String location) {
	this.location = location;
}

public Date getBirthDate() {
	return birthDate;
}

public void setBirthDate(Date birthDate) {
	this.birthDate = birthDate;
}

@Override
public String toString() {
	return String.format("\nPerson [id=%s, name=%s, location=%s, birthDate=%s]", id, name, location, birthDate);
}

}

package com.ayush.jpa.JpaDemo;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.springframework.stereotype.Repository;

@repository
@transactional
public class PersonRepository {

@PersistenceContext
EntityManager manager;

public Person findById(int id) {
	return manager.find(Person.class, id);
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant