简体   繁体   中英

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. 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
@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.

PS: **Why after execute this code in controller tables in MySql are 'cleaning' ? **

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM