已解决:问题在于周围方面以某种方式修改了代理中的返回值。抱歉这个无法回答的问题。也许有人知道我的错。感谢您提供有用的答案!
我的 Spring Boot 控制台应用程序有问题。我还使用 spring data jpa 来持久化。一切工作正常,我可以保存和查找实体,但是当服务类方法返回实体时,它将被分离。当我想使用返回的对象时,应用程序失败,因为对象值为空。
这只是一个例子。
@Entity
@Table(name = "person")
public class Person {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private int id;
private String name;
@Convert(converter = LocalDateAttributeConverter.class)
private LocalDate birth;
public Person() {
}
//getter, setter..
}
public interface PersonRepository extends CrudRepository<Person, Long> {
public Person findByName(String name);
}
public class Service {
@Autowired
private PersonRepository personRepository;
public Person findPersonByName(String name) {
return personRepository.findByName(name); // System.out.println(person.name) -> someone
}
}
public class App {
private Service service;
public void doSomething(){
Person person = service.findPersonByName("someone");
// System.out.println(person.name) -> nullpointerEx
}
}
//The player like person in the exaple. I must cover some detail in the package names.
//This is the real exception. As a say above, the code it's just a raw example. Probably don't match for this.
Hibernate: select player0_.id as id1_7_, player0_1_.email as email2_7_, player0_1_.password as password3_7_, player0_.balance as balance1_4_, player0_.birth as birth2_4_, player0_.currency as currency3_4_, player0_.name as name4_4_ from player player0_ inner join user player0_1_ on player0_.id=player0_1_.id where player0_.name=?
14:24:02.087 [main] INFO o.s.b.a.l.ConditionEvaluationReportLoggingListener -
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
14:24:02.103 [main] ERROR o.s.boot.SpringApplication - Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:787)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:768)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at com....Application.main(..Application.java:13)
Caused by: java.lang.NullPointerException: null
at com.....view.ConsoleView.printWelcomeMassege(ConsoleView.java:101)
at com.....App.createPersone(App.java:46)
at com.....App.play(App.java:33)
at com....Application.lambda$0(..Application.java:22)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:784)
... 5 common frames omitted
14:24:02.118 [main] INFO o.s.o.j.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'default'
14:24:02.118 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
14:24:02.165 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.
我该如何处理这个问题?
最佳答案
我认为您缺少@Column(name = "name")
注释
关于java - 为什么实体对象在方法返回后会被分离? ( Spring 数据JPA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60339218/