我正在eclipse中创建一个JPA应用程序。我用spring设计了服务层。我有 用过" HibernatePersistence"作为持久性提供者。 下面是我的持久层:
@MappedSuperclass
public class BaseEntity implements Serializable{
/**
*
*/
@Transient
private static final long serialVersionUID = -2265881632984808864L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="ID",nullable=false,updatable=false)
private Long id;
/**
* @return the id
*/
public Long getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}
}
package com.edfx.jpapp.persist.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name = "Customer", schema = "customerdb")
public class Customer extends BaseEntity {
/**
*
*/
@Transient
private static final long serialVersionUID = 1443119926544372161L;
@Column(name = "CUSTOMER_NAME", nullable = false, length = 30)
private String customerName;
@Column(name = "CUSTOMER_ADDRESS", nullable = false, length = 25)
private String customerAddress;
/**
* @return the customerName
*/
public String getCustomerName() {
return customerName;
}
/**
* @param customerName
* the customerName to set
*/
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
/**
* @return the customerAddress
*/
public String getCustomerAddress() {
return customerAddress;
}
/**
* @param customerAddress
* the customerAddress to set
*/
public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}
/**
* @return the serialversionuid
*/
public static long getSerialversionuid() {
return serialVersionUID;
}
}
这是我的DAO图层
package com.edfx.dao.impl;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.edfx.jpapp.dao.CustomerDAO;
import com.edfx.jpapp.persist.entity.Customer;
public class CustomerDAOImpl implements CustomerDAO {
@PersistenceContext(unitName="customerUnit")
private EntityManager entityManager;
/**
* @return the entityManager
*/
public EntityManager getEntityManager() {
return entityManager;
}
/**
* @param entityManager
* the entityManager to set
*/
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public void insertCustomer(Customer customer) {
entityManager.persist(customer);
}
@Override
public List<Customer> getAllCustomer() {
return entityManager.createQuery("select c from customer c",
Customer.class).getResultList();
}
}
here is the service layer
package com.edfx.jpapp.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.dozer.Mapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.edfx.jpapp.dto.CustomerDTO;
import com.edfx.jpapp.persist.entity.Customer;
import com.edfx.jpapp.service.CustomerService;
import com.edfx.jpapp.service.base.BaseService;
@Transactional
public class CustomerServiceImpl extends BaseService implements CustomerService {
@Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Throwable.class })
@Override
public void insertCustomer(CustomerDTO customerDTO) {
Customer customer = getBeanMapper().map(customerDTO, Customer.class);
getDaoProvider().getCustomerDAO().insertCustomer(customer);
}
@Transactional(readOnly = true)
@Override
public List<CustomerDTO> getAllCustomer() {
Mapper mapper = getBeanMapper();
ArrayList<CustomerDTO> customerDTOs = new ArrayList<CustomerDTO>();
List<Customer> customers = getDaoProvider().getCustomerDAO()
.getAllCustomer();
for (Customer customer : customers) {
CustomerDTO customerDTO = mapper.map(customer, CustomerDTO.class);
customerDTOs.add(customerDTO);
}
return customerDTOs;
}
}
我已将persistence.xml文件放在META-INF文件夹下:它如下
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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_2_0.xsd">
<persistence-unit name="customerUnit"
transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jBossCustomerMysql</jta-data-source>
<class>com.edfx.jpapp.persist.entity.Customer</class>
<validation-mode>NONE</validation-mode>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
我使用过JTA DataSource,并在jboss standalone.xml文件中配置了JNDI 如下:
<datasource jta="true" jndi-name="java:/jBossCustomerMysql" pool-name="jBossCustomerMysql" enabled="true" use-ccm="false">
<connection-url>jdbc:mysql://localhost:3306/customerdb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password>root</password>
</security>
<validation>
<validate-on-match>false</validate-on-match>
<background-validation>false</background-validation>
</validation>
<statement>
<share-prepared-statements>false</share-prepared-statements>
</statement>
</datasource>
当我运行应用程序时,我的表在数据库中创建。但是当我尝试从xhtml页面插入客户时,我得到了异常::
13:52:37,227 SEVERE [javax.faces.event] (http-localhost-127.0.0.1-8087-2) Received 'org.springframework.transaction.CannotCreateTransactionException' when invoking action listener '#{customerController.addCustomer}' for component 'j_idt9'
13:52:37,227 SEVERE [javax.faces.event] (http-localhost-127.0.0.1-8087-2) org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NullPointerException
at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:427)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371)
at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:329)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at com.sun.proxy.$Proxy22.insertCustomer(Unknown Source)
at com.edfx.jpapp.web.controller.CustomerController.addCustomer(CustomerController.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148)
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
以下是我的弹簧配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="customerUnit" />
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml" />
<property name="packagesToScan" value="com.edfx.jpapp.persist.entity" />
</bean>
</beans>
另一个是transactionConfig.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean id="transactionInterceptor" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributeSource">
<bean class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
任何人都可以给出任何解决方案吗??????
答案 0 :(得分:0)
我找到了解决方案。我需要将以下代码添加到我的persistence.xml文件中。
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>