[英]JPA Entities are not managed / relations not persisted
我在JBoss 4.3.2上使用JPA 1.0,Spring 3.0.6和Hibernate 3.2.1。 有一些EJB在某些時候稱為DAO,它試圖持久化兩個實體。
結果非常出乎意料: 在調用.persist(entity)之后,不對實體進行管理 。
有趣的是:一個非常相似的測試應用程序,使用與服務器應用程序相同的jar,其中包含DAO,Beans persistence.xml和服務器應用程序,可以將所有內容持久保存。 在調用.persist()
之后對bean進行管理。
我有一個類Subscriber
具有SubscriberState
。
@Entity
@Table(name = "subscriber")
public class Subscriber implements java.io.Serializable {
// all the other stuff
@ManyToOne
@JoinColumn(name = "status")
private SubscriberState state;
}
出於遺留原因, SubscriberState
具有一個Subscriber
關系。
@Entity
@Table(name = "subscriber_state")
public class SubscriberState implements java.io.Serializable {
// ...blah
@ManyToOne
@JoinColumn(name = "subscriber", nullable = false)
private Subscriber subscriber;
}
現在,我嘗試保留訂戶,而在persist()之后將不再對其進行管理:
final Subscriber subscriber = new Subscriber();
// set up...
entityManager.persist(subscriber);
entityManager.contaons(subscriber); //will yield FALSE
添加SubscriberState
將起作用。 但是關系的只有一側將被存儲到數據庫:
SubscriberState subscriberState = new SubscriberState();
subscriberState.setSubscriber(subscriber);
entityManager.persist(subscriberState);
entityManager.merge(subscriber);
其中SubscriberState.setSubscriber
如下所示:public void setSubscriber(final Subscriber Subscriber){this.subscriber = Subscriber; subscription.setState(this); }
查看數據庫可以看到, SubscriberState
知道它是Subscriber
,但是訂戶不知道它是State
。
這是persistence.xml(現在已修改並精簡):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
org.hibernate.ejb.HibernatePersistence java:jdbc / SODS
<properties>
<property name="hibernate.dialect" value="${db.hibernate.dialect}" />
<property name="hibernate.validator.apply_to_ddl" value="false" />
<property name="hibernate.validator.autoregister_listeners" value="false" />
<property name="hibernate.cache.provider_class" value="org.jboss.ejb3.entity.TreeCacheProviderHook" />
<property name="hibernate.treecache.mbean.object_name" value="jboss.cache:service=EJB3EntityTreeCache" />
<property name="jboss.entity.manager.jndi.name" value="java:/SOEntityManager-${app.version}" />
<property name="jboss.entity.manager.factory.jndi.name" value="java:/SOEntityManagerFactory-${app.version}" />
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory" />
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />
</properties>
<class>com.stackoverflow.some.classes.i.cant.show.Here</class>
這是我春季內容的相關部分:
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
<jee:jndi-lookup id="entityManagerFactory" jndi-name="java:/SOEntityManagerFactory-${app.version}" />
<jee:jndi-lookup id="entityManager" jndi-name="java:/SOEntityManager-${app.version}" />
<jee:jndi-lookup id="soDataSource" jndi-name="java:jdbc/SODS" />
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="soDataSource" />
</bean>
<context:component-scan base-package="blah - secret" />
<import resource="daoContext.xml" />
</beans>
有什么想法,為什么這個配置不起作用?
我在您的DAO或服務層中看不到@Transaction的使用
添加SubscriberState將起作用。 但是關系的只有一側將存儲到數據庫
您應該在使用層疊選項
@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "subscriber", nullable = false)
private Subscriber subscriber;
這樣可以將雙方關系保存在持久操作中
另外我看到的是兩個單向關系,而不是一個雙向關系,嘗試使用mappedBy
@ManyToOne
@JoinColumn(name = "status")
private SubscriberState state;
除非那不是您想要的,否則您應該重新考慮設計以使一側為OneToMany,另一側為ManyToOne,請使用mappingBy定義關系的源(所有者)
這發生在我的應用程序(使用MySQL的JPA)上。 在屬性文件中加載適當的數據庫驅動程序類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.