0
首先是一對多的關係,請看看我的設計,Hibernate的註釋,問題與複合主鍵
我有兩個表 T1(A,B,C,d) - 一個是主鍵 T2(a,e)其中(a,e)是複合主鍵 a是外鍵引用T1(a)
T1-T2關係是一對多關係。
因此,我創建三個類
C1爲T1 它有五個字段 - A,B,C,d,c2Set與設置器&吸氣劑
C2爲T2 它具有兩個字段 - 分別爲C1,C3的c1,c3 - 與設置者&獲取者
C3我正在定義C3來表示複合T2 的主鍵它有兩個字段(A,E)與設置器&吸氣劑
我使用以下注釋在類C1,C2,以限定休眠映射,C3分別
1.
@Entity
@Table(name = "T1", schema = "dbo", uniqueConstraints = { @UniqueConstraint(columnNames = "a") })
@Id
@Column(name = "a", unique = true, nullable = false)
@Transient
private Set<C2> c2Set = new HashSet<C2>();
@OneToMany(fetch = FetchType.EAGER, mappedBy = "c1")
public Set<C2> getC2Set() {
return c2Set;
}
2.
@Entity
@Table(name="T2", schema = "dbo")
@Id
public C3 getC3() {
return c3;
}
private C1 c1;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "a",insertable= false, updatable=false)
public C1 getC1() {
return c1;
}
3.
@Embeddable
@Column(name = "a")
public String getA() {
return a;
}
@Column(name="e")
public String getE() {
return e;
}
@Service
public class C1ServiceImpl implements C1Service {
@Autowired
@Qualifier("transactionProxy")
private GenericDAO genericDAO;
public GenericDAO getGenericDAO() {
return genericDAO;
}
public void setGenericDAO(GenericDAO genericDAO) {
this.genericDAO = genericDAO;
}
@Override
public void registerC1() {
C1 c1=new C1();
c1.setA("aValue");
c1.setB("someValue");
c1.setC("someValue");
c1.setD("someValue");
C3 compositeKey1 = new C3();
compositeKey1.setA("aValue");
compositeKey1.setE("e1");
C3 compositeKey2 = new C3();
compositeKey1.setA("aValue");
compositeKey1.setE("e2");
C2 c2_1=new C2();
c2_1.setC3(compositeKey1);
C2 c2_2=new C2();
c2_2.setC3(compositeKey2);
Set<C2> eSet = new HashSet<C2>();
eSet.add(c2_1);
eSet.add(c2_2);
c1.setRoles(eSet);----------------------Important
genericDAO.registerUser(c1);
}
}
@Repository
@Transactional
public class HibernateDAO implements GenericDAO {
@Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session currentSession() {
return sessionFactory.getCurrentSession();
}
@Override
public void registerC1(C1 c1) {
Session session = currentSession();
session.beginTransaction();
session.save(c1);----------------------------------------- Here I feel insertion should happen in both T1 & T2 tables. What should I should I do, should I insert into T2 in the same way as I am doing for T1
session.getTransaction().commit();
}
}
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url"
value="XXXXXXXXXXXXXXXXXXXXXXX" />
<property name="username" value="dddddd" />
<property name="password" value="eeeeee" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="<package in which C1,C2,C3,Generic DAO, HibernateDAO live>" />
<property name="annotatedClasses">
<list>
<value>Fully qualified name of C1</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory
</prop>
<prop key="dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="myTxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="transactionProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyTargetClass" value="true" />
<property name="transactionManager">
<ref bean="myTxManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<property name="target">
<bean class="fully qualified name of HibernateDAO">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</property>
</bean>
我在插入(保存)數據時遇到問題。數據到T1是越來越插入但沒有插入在T2(我認爲這裏應該自動發生)發生了什麼? 請告訴我在哪裏,我錯誤地做事情。 而且,我想了解更多關於Hibernate註解。任何人都可以點我一些有用的在線資源。
而且,它的好處是從實體類表單bean分開? 請告訴我可以瞭解的資源