如何使用复合键在@OneToMany关系中保留多行

时间:2016-11-20 11:39:45

标签: jpa

我找到了解决我的数据库架构的 OLD 链接:

http://randomthoughtsonjavaprogramming.blogspot.com.es/2014/09/jpa-manytoone-with-composite-primary-key.html

我可以检索数据,一切都很好,但我不知道如何在发布给作者时插入数据。

在我的情况下插入每个公会我需要同时插入几个guildranks,所以一个用例将是:

BEGIN TRANSACTION

GUILD

Guildname1

GUILDRANK

Guildname1, 1
Guildname1, 2
Guildname1, 3
Guildname1, 4

结束交易

有什么建议吗? 感谢

更新

@Entity
@NamedQueries({
        @NamedQuery(name = "Cuestionario.findAll", query = "SELECT c FROM Cuestionario c")
 })
public class Cuestionario implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @OneToMany(mappedBy = "cuestionario", cascade = { CascadeType.PERSIST })
    private Set<Hoja> hojas;

}



@Entity
@NamedQuery(name = "Hoja.findAll", query = "SELECT h FROM Hoja h")
public class Hoja implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private HojaPK id;

    // bi-directional many-to-one association to Cuestionario
    @ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
    @JoinColumn(name = "ID", insertable = false, updatable = false)
    private Cuestionario cuestionario;

    public Hoja() {
    }

}

@Embeddable
public class HojaPK implements Serializable {
    // default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;


    //Correspond with "id" in Cuestionario 
    @Column(name = "ID")
    private long id;

    private int dia;

    public HojaPK() {
    }
}   

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="pu"
        transaction-type="RESOURCE_LOCAL">


        <properties>

            <property name="javax.persistence.jdbc.user" value="***" />
            <property name="javax.persistence.jdbc.password" value="***" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:sqlserver://localhost:1433;databaseName=mydatabase" />
            <property name="javax.persistence.jdbc.driver"
                value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />


            <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
            <property name="hibernate.show_sql" value="true" />
            <!--  
            <property name="hibernate.hbm2ddl.auto" value="update" />
            -->
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
            <property name="hibernate.connection.charSet" value="UTF-8" />
        </properties>

    </persistence-unit>

</persistence>

TestCase.java

EntityManagerFactory emf = Persistence.createEntityManagerFactory("pu");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();

        em.persist(cuestionario);
        em.flush();
        for (int i = 0; i <= 32; i++) {
            HojaPK hojaPk = new HojaPK(cuestionario.getId(), i);
            Hoja hoja = new Hoja();
            hoja.setId(hojaPk);
            em.persist(hoja);
        }


em.getTransaction().commit();
em.close();
emf.close();

0 个答案:

没有答案