[英]Spring MVC + JPA - Null Pointer Exception
I've lost patience. 我失去了耐心。 I'm trying to turn on JPA for the first time and I still get Null Pointer Exception ... I do not know what to do next.
我正在尝试首次打开JPA,但仍然会收到Null Pointer Exception(空指针异常)...我不知道下一步该怎么做。 Please help.
请帮忙。 I try to do it like in a book "Spring in action".
我尝试像《行动中的春天》那样做。
@Configuration
@EnableJpaRepositories(basePackages = {"org.project"})
public class JpaConfig {
private DataSource dataSource;
@Autowired
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("org.project.persistance");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
return new PersistenceExceptionTranslationPostProcessor();
}
private Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
return properties;
}
Repository: 仓库:
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
@Repository
public class DogRepositoryImpl implements DogRepository {
@PersistenceUnit
private EntityManagerFactory entityManager;
@Override
public Dog findById(long id) {
return entityManager.createEntityManager().find(Dog.class, id);
}
@Override
public void persist(Dog dog) {
entityManager.createEntityManager().persist(dog);
}
}
And @Entity: 和@Entity:
@Entity
@Table(name = "dogs")
public class Dog {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String owner;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
}
Please help :( When I tried to do it with Spring Data, it was the same. 请帮助:(当我尝试使用Spring Data进行操作时,它是相同的。
PS: **Why after execute this code in controller tables in MySql are 'cleaning' ? PS:**为什么在MySql的控制器表中执行此代码后会“清理”? **
**
It should be caused by this line: 这应该是由以下行引起的:
properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
Once the session factory is closed, the tables will all be dropped. 会话工厂关闭后,所有表将被删除。 You can change it to
create
so that the tables will persist. 您可以对其进行更改以进行
create
以便表能够持久存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.