Hibernate注释 - 与复合主键的一对多关系的问题

时间:2011-02-27 22:17:15

标签: hibernate spring

首先,请好好看看我的设计,

我有两张桌子 T1(a,b,c,d) - a是主键 T2(a,e)其中(a,e)是复合主键              a是引用T1(a)的外键

T1 - T2关系是一对多。

所以我创建了三个类

  1. C1为T1 它有五个领域 - a,b,c,d,c2Set与setter&吸气剂

  2. T2为T2 它有两个字段-c1,c3,分别为C1,C3,具有setter&吸气剂

  3. C3我定义C3来表示T2的复合主键 它有两个字段(a,e),带有setter和amp;吸气剂

  4. 我使用以下注释分别在C1,C2,C3类中定义hibernate映射

    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 Annotations的更多信息。任何人都可以向我指出一些有用的在线资源。

    而且,将表单bean与实体类分开是否合适? 请告诉我我可以了解的资源

1 个答案:

答案 0 :(得分:0)

默认情况下,Hibernate不会持久保存由关系引用的对象。您需要配置级联关系以使Hibernate执行此操作:

@OneToMany(fetch = FetchType.EAGER, mappedBy = "c1", cascade = CascadeType.ALL)
public Set<C2> getC2Set() {
    return c2Set;
} 

另见: