Java EJB3映射分离的实体传递到持久化
使用Arquillan运行集成测试时,我得到以下错误:org.hibernate.PersistenceException:org.hibernate.PersistentObjectException:传递给persistent的分离实体。可能是我的身份证和数据库 以下是它正在抱怨其属于域类的类:Java EJB3映射分离的实体传递到持久化,java,hibernate,jpa,jboss-arquillian,Java,Hibernate,Jpa,Jboss Arquillian,使用Arquillan运行集成测试时,我得到以下错误:org.hibernate.PersistenceException:org.hibernate.PersistentObjectException:传递给persistent的分离实体。可能是我的身份证和数据库 以下是它正在抱怨其属于域类的类: @Entity public class Customer implements IdHolder { @Id @GeneratedValue(strategy = Generat
@Entity
public class Customer implements IdHolder {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String firstName;
private String lastName;
private String email;
private String company;
public Customer() {
}
public Customer(long id, String firstName, String lastName, String email,
String company) {
setId(id);
setFirstName(firstName);
setLastName(lastName);
setEmail(email);
setCompany(company);
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
}
我还有一个testfixture使测试更容易,它的实现方式如下:
public class TestFixture {
private static Logger log = Logger.getLogger(TestFixture.class.getName());
public static Customer getCustomer(long id, String firstName,
String lastName, String email, String company) {
Customer customer = new Customer();
customer.setId(id);
customer.setFirstName(firstName);
customer.setLastName(lastName);
customer.setEmail(email);
customer.setCompany(company);
return customer;
}
public static Customer getCustomer() {
return getCustomer(1, "Darth", "Vader", "skywalker@gmail.com", "Starwars");
}
public static Customer getCustomer(String name, String lastName, String email, String company) {
return getCustomer(0, name, lastName, email, company);
}
public static Archive<?> createIntegrationTestArchive() {
MavenDependencyResolver mvnResolver = DependencyResolvers.use(
MavenDependencyResolver.class).loadMetadataFromPom("pom.xml");
WebArchive war = ShrinkWrap.create(WebArchive.class, "agent_test.war")
.addPackages(true, "se.lowdin")
.addPackages(true, "se.plushogskolan")
.addAsWebInfResource("beans.xml")
.addAsResource("META-INF/persistence.xml");
war.addAsLibraries(mvnResolver.artifact("org.easymock:easymock:3.2")
.resolveAsFiles());
war.addAsLibraries(mvnResolver.artifact("joda-time:joda-time:2.2")
.resolveAsFiles());
war.addAsLibraries(mvnResolver.artifact(
"org.jadira.usertype:usertype.core:3.1.0.CR8").resolveAsFiles());
log.info("JAR: " + war.toString(true));
return war;
}
}
及
公共抽象类JpaRepository实现BaseRepository{
/**
*此存储库可以处理的JPA类型。仅在运行时已知。此
*值在构造函数中设置。
*/
保护类实体类;
@持久上下文
受保护的实体管理器em;
@抑制警告(“未选中”)
公共JpaRepository(){
/*
*一个小魔术,看看超类,找到我们的类型
*例如,我们在findById()中使用该类型。
*/
ParameteredType genericSuperclass=(ParameteredType)getClass().getGenericSuperclass();
this.entityClass=(类)genericSuperclass.getActualTypeArguments()[0];
}
@凌驾
公共长存留(E实体){
em.persist(实体);
返回entity.getId();
}
@凌驾
公共无效删除(E实体){
em.remove(实体);
}
@凌驾
公共E findById(长id){
返回em.find(entityClass,id);
}
@凌驾
公共作废更新(E实体){
合并(实体);
}
}
当我无法解决这个问题时,我真的觉得自己像个白痴。有人能帮我解释一下问题吗?您可以将
Customer.id
从long
更改为long
。在持久化实体之前,只需将Id设置为null
。这应该会有所帮助。发布TestFixture.getCustomer()方法;CustomerIt的映射应该已经发布了对不起,错过了:)你在使用什么RDBMS?请发布repo.persist()我现在发布了persist方法。我正在使用MySql
@RunWith(Arquillian.class)
@Transactional(TransactionMode.ROLLBACK)
public class JpaCustomerIntegrationTest extends AbstractRepositoryTest<Customer, JpaCustomerRepository> {
@Inject JpaCustomerRepository repo;
@Test
public void testGetAllCustomers() {
Customer customer1 = TestFixture.getCustomer();
Customer customer2 = TestFixture.getCustomer();
customer1.setId(0);
customer2.setId(0);
repo.persist(customer1);
repo.persist(customer2);
List<Customer> getAllCustomersList = repo.getAllCustomers();
assertEquals("Check the amount from the list", 2, getAllCustomersList.size());
}
@Override
protected JpaCustomerRepository getRepository() {
return (JpaCustomerRepository) repo;
}
@Override
protected Customer getEntity1() {
return TestFixture.getCustomer();
}
@Override
protected Customer getEntity2() {
return TestFixture.getCustomer();
}
}
public abstract class JpaRepository<E extends IdHolder> implements BaseRepository<E> {
/**
* The JPA type this repository can handle. Only known at runtime. This
* value is set in the constructor.
*/
protected Class<E> entityClass;
@PersistenceContext
protected EntityManager em;
@SuppressWarnings("unchecked")
public JpaRepository() {
/*
* A little magic to look into the superclass to find the type we are
* working on. We use that type in findById() for example .
*/
ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
this.entityClass = (Class<E>) genericSuperclass.getActualTypeArguments()[0];
}
@Override
public long persist(E entity) {
em.persist(entity);
return entity.getId();
}
@Override
public void remove(E entity) {
em.remove(entity);
}
@Override
public E findById(long id) {
return em.find(entityClass, id);
}
@Override
public void update(E entity) {
em.merge(entity);
}
}