使用Hibernate和Spring进行一对多映射

曼诺·苏塔尔(Manoj suthar)

我正在尝试使用外键创建两个联接表ConceptModelDetails和Instructions。以下是我的模型课程:

ConceptModel详细信息:

package com.assignment.model;

@Entity
@Table(name="conceptModelDetails")
public class ConceptModelDetails {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private int instructionsId;
    private String operationType;
    private String conceptModelID;
    private String requestor;
    private String status;
    private Timestamp requestDateTime;
    private Timestamp lastExecutedDateTime;
    private Timestamp completedDateTime;
    @OneToMany(cascade=CascadeType.ALL, mappedBy="conceptModelDetails")
    private Set<Instructions> instructions; 

    public ConceptModelDetails() {}     
}

和说明:

package com.assignment.model;

@Entity
@Table(name="instructions")
public class Instructions {
    @Id
    @GeneratedValue
    private int Sno;
    private String instruction;
    @ManyToOne
    @JoinColumn(name="instructionsId")
    private ConceptModelDetails conceptModelDetails;
}

以下是applicationContext.xml

<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-4.1.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd 
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://192.168.1.79:5432/test" />
        <property name="username" value="postgres" />
        <property name="password" value="admin" />
    </bean>
    <bean id="objDAO" class="com.assignment.dao.impl.ConceptModelDAOImpl">
        <property name="sessionFactory" ref="sessionfactory" />

    </bean>
    <bean id="sessionfactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="packagesToScan" value="com.assignment.model"></property>
        <property name="annotatedClasses">
            <list>
                <value>com.assignment.model.ConceptModelDetails</value>
                <value>com.assignment.model.Instructions</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.cache.use_second_level_cache">false</prop>
            </props>
        </property>

    </bean>
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionfactory" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

和控制器:

    @RequestMapping(value = "/myCntrl", method = RequestMethod.POST)
    public String handler(HttpServletRequest request) {
        System.out.println("handler");
        // System.out.println(request.getParameter("conceptID"));
        // System.out.println(request.getParameter("operationType"));
        String[] operations = request.getParameterValues("operations");
        Date date = new Date();
        Timestamp time = new Timestamp(date.getTime());
        ConceptModelDetails conceptModelDetails = new ConceptModelDetails();
        conceptModelDetails
                .setConceptModelID(request.getParameter("conceptID"));
        conceptModelDetails.setOperationType(request
                .getParameter("operationType"));
        conceptModelDetails.setRequestor(request.getParameter("requestor"));
        conceptModelDetails.setRequestDateTime(time);
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        System.out.println("yo");
        ConceptModelDAO obj = (ConceptModelDAO)context.getBean("objDAO");
        System.out.println("no");
        Instructions instructions = new Instructions();
        for(int i = 0; i < operations.length; i++){
        instructions.setInstruction(operations[i]);
        obj.addInstructions(instructions);
        }
        obj.add(conceptModelDetails);           


        return "success";

    }

运行此代码时出现的问题是:

  1. 两个表都使用相同的hibernate_sequence。

  2. 如以下屏幕快照所示,外键未映射在指令表中。

桌子

请指导该代码出了什么问题。我是冬眠和春季的新手,所以希望您能得到详细的解释。提前致谢。

tharindu_DG

1)如果没有按JPA规范指定的生成器,则使用默认情况下休眠的全局序列生成器。

如下更改。对两个类都执行此操作,每个类的顺序不同。

@Entity
@SequenceGenerator(name="PRIVATE_SEQ", sequenceName="private_sequence")
public class ConceptModelDetails {
    @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="PRIVATE_SEQ")
    private int instructionsId;

2)在这里Instructions类拥有关系,因此,你应该设定ConceptModelDetails目标为Instructions保存前的对象Instructions对象。

ConceptModelDetails cmd1 = new ConceptModelDetails();

Instructions i = new Instructions()
i.setConceptModelDetails(cmd1);
....

然后保存Instructions对象i

希望这可以帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章