Project正在编译,但在浏览器发出请求时会出错:
ava.lang.NullPointerException
at org.belarusgo.repository.jpa.JpaNewsRepositoryImpl.findById(JpaNewsRepositoryImpl.java:24)
at org.belarusgo.repository.jpa.JpaNewsRepositoryImpl.findById(JpaNewsRepositoryImpl.java:12)
at org.belarusgo.service.BelarusGoServiceImpl.findNewsById(BelarusGoServiceImpl.java:48)
还有一个日志:
16:13:29,455 DEBUG JpaTransactionManager:371 - Opened new EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@4e54a245] for JPA transaction
16:13:29,459 DEBUG DataSourceUtils:153 - Setting JDBC Connection [Pooled connection wrapping physical connection org.postgresql.jdbc.PgConnection@5d7ad29] read-only
16:13:29,460 DEBUG TransactionImpl:51 - begin
16:13:29,461 DEBUG JpaTransactionManager:403 - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@4f50eb69]
16:13:29,462 DEBUG BelarusGoServiceImpl:47 - From findNewsById(3)
16:13:29,463 DEBUG JpaTransactionManager:851 - Initiating transaction rollback
16:13:29,463 DEBUG JpaTransactionManager:538 - Rolling back JPA transaction on EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@4e54a245]
16:13:29,463 DEBUG TransactionImpl:86 - rolling back
16:13:29,464 DEBUG DataSourceUtils:222 - Resetting read-only flag of JDBC Connection [Pooled connection wrapping physical connection org.postgresql.jdbc.PgConnection@5d7ad29]
16:13:29,465 DEBUG JpaTransactionManager:600 - Closing JPA EntityManager [org.hibernate.jpa.internal.EntityManagerImpl@4e54a245] after transaction
16:13:29,466 DEBUG EntityManagerFactoryUtils:435 - Closing JPA EntityManager
16:13:29,468 DEBUG ExceptionHandlerExceptionResolver:133 - Resolving exception from handler [public org.springframework.web.servlet.ModelAndView org.belarusgo.web.controller.NewsController.findNews(int)]: java.lang.NullPointerException
16:13:29,469 DEBUG ResponseStatusExceptionResolver:133 - Resolving exception from handler [public org.springframework.web.servlet.ModelAndView org.belarusgo.web.controller.NewsController.findNews(int)]: java.lang.NullPointerException
16:13:29,470 DEBUG DefaultHandlerExceptionResolver:133 - Resolving exception from handler [public org.springframework.web.servlet.ModelAndView org.belarusgo.web.controller.NewsController.findNews(int)]: java.lang.NullPointerException
16:13:29,471 DEBUG DispatcherServlet:993 - Could not complete request
java.lang.NullPointerException
at org.belarusgo.repository.jpa.JpaNewsRepositoryImpl.findById(JpaNewsRepositoryImpl.java:24)
at org.belarusgo.
repository.jpa.JpaNewsRepositoryImpl.findById(JpaNewsRepositoryImpl.java:12)
的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="belarusgo" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.connection.driver_class"
value="org.postgresql.Driver"/>
<property name="hibernate.connection.url"
value="jdbc:postgresql://localhost:5432"/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.dialect"
value="org.hibernate.dialect.PostgreSQL9Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
WebConfig:
@Bean
public DataSource getDataSource() {
PGPoolingDataSource dataSource = new PGPoolingDataSource();
dataSource.setDataSourceName("Postgresql datasource");
dataSource.setServerName("localhost");
dataSource.setPortNumber(5432);
dataSource.setDatabaseName("belarusgo");
dataSource.setUser("postgres");
dataSource.setPassword("1234");
dataSource.setMaxConnections(100);
dataSource.setInitialConnections(5);
return dataSource;
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("org.belarusgo.model.domain");
factory.setDataSource(getDataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
新闻的BaseEntity:
@MappedSuperclass
public class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
protected Integer id;
新闻:
@Entity
@Table(name = "news")
public class News extends BaseEntity {
@Column(name = "published_date")
//@Temporal(TemporalType.DATE)
private Date publishedDate;
@Column(name = "headline", length = 200)
@NotNull
@Size(min=5, max=200)
private String headline;
@Column(name = "author", length = 100)
@NotNull
@Size(min=5, max=100)
private String author;
@Column(name = "text")
@NotNull
@Size(min=20)
private String text;
服务实施:
@Service
@Transactional
public class BelarusGoServiceImpl implements BelarusGoService{
private PlayerRepository playerRepository;
private TournamentRepository tournamentRepository;
private NewsRepository newsRepository;
@Autowired
public BelarusGoServiceImpl(PlayerRepository playerRepository,
TournamentRepository tournamentRepository,
NewsRepository newsRepository) {
this.playerRepository = playerRepository;
this.tournamentRepository = tournamentRepository;
this.newsRepository = newsRepository;
}
@Override
@Transactional(readOnly = true)
public News findNewsById(int id) {
logger.debug("From findNewsById({})", id);
return newsRepository.findById(id);
}
存储库实现:
public class JpaNewsRepositoryImpl implements NewsRepository {
@PersistenceContext(unitName = "belarusgo")
private EntityManager em;
@Override
public News findById(Integer id) {
return this.em.find(News.class, id);
}