我试图使用外键创建两个连接表ConceptModelDetails和指令。以下是我的模型类:
概念模型详细信息:

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";

    }

运行此代码时出现的问题有:
两个表使用相同的hibernate_序列。
外键未映射到指令表中,如下图所示。
请告诉我密码有什么问题。我对冬眠和春天还不熟悉,所以我希望能有一个详细的解释。提前谢谢。

最佳答案

1)您使用的是hibernate默认提供的全局序列生成器,而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
希望这有帮助。

09-10 07:00