JPA与OneToMany和ManyToOne之间的关系

时间:2012-10-25 08:27:40

标签: java jpa entity-relationship

我是JPA的新手并尝试在这两个表之间建立关系:

这是主类对象XParserLinks

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "link_id")
    private Integer linkId;
    @OneToMany(mappedBy = "xParserLink", targetEntity = XLinksMedia.class, cascade = CascadeType.ALL)
    private List<XLinksMedia> fotos;
    ...

这是对象类XLinksMedia

public class XLinksMedia implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected XLinksMediaPK xLinksMediaPK;
    @ManyToOne @JoinColumn(name = "link_id")
    private XParserLinks xParserLink;

XLinksMediaPK

@Embeddable
public class XLinksMediaPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "link_id", insertable = false, updatable = false)
    private int linkId;
    @Basic(optional = false)
    @Column(name = "image")
    private String image;

这些关系是否正确?因为当我想在XLinksMedia列表中添加新的fotos个对象时,我得到LinkId = 0,但我知道LinkId不是0,我知道{ {1}}变量有一个值。

以下是我尝试在LinkId类中设置fotos变量的方式XParseLinkspreke对象是否有他的主键XParseLinks):

LinkId

1 个答案:

答案 0 :(得分:0)

即使我不知道你的期望是什么,当我测试你的编码时,它是可以的。我只是将一些OverLoad Constructor放在你的类中进行简单的数据初始化。但是,您使用GenerationType.IDENTITY,这就是为link_id XParserLinks设置自动递增列的原因。以下编码的主要内容与您的相同。

您的观点Here's how I try to set fotos variable in XParseLinks class (preke: is XParseLinks object witch have his primary key LinkId):。检查Test.java

XParserLinks.java

@Entity
public class XParserLinks implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "link_id")
    private Integer linkId;
    @OneToMany(mappedBy = "xParserLink", targetEntity = XLinksMedia.class, cascade = CascadeType.ALL)
    private List<XLinksMedia> fotos;

    private String something;

    public XParserLinks() {
        fotos = new ArrayList<XLinksMedia>();
    }

    public XParserLinks(String something) {
        this.something = something;
        fotos = new ArrayList<XLinksMedia>();
    }
    //getter and setter
}       

XLinksMediaPK.java

@Embeddable
public class XLinksMediaPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "link_id", insertable = false, updatable = false)
    private int linkId;
    @Basic(optional = false)
    @Column(name = "image")
    private String image;

    public XLinksMediaPK(){
    }

    public XLinksMediaPK(int linkId, String image) {
        this.linkId = linkId;
        this.image = image;
    }
    //getter and setter
}

XParserLinks.java

@Entity
public class XParserLinks implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "link_id")
    private Integer linkId;
    @OneToMany(mappedBy = "xParserLink", targetEntity = XLinksMedia.class, cascade = CascadeType.ALL)
    private List<XLinksMedia> fotos;

    private String something;

    public XParserLinks() {
        fotos = new ArrayList<XLinksMedia>();
    }

    public XParserLinks(String something) {
        this.something = something;
        fotos = new ArrayList<XLinksMedia>();
    }
    //getter and setter

    public void add(XLinksMedia media) {
        fotos.add(media);
    }
}

Test.java

public class Test {
    public static void main(String[] args) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPA");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        XParserLinks xParserLink = new XParserLinks("something");

        XLinksMediaPK pk1 = new XLinksMediaPK(0, "image_1");
        XLinksMediaPK pk2 = new XLinksMediaPK(0, "image_2");
        XLinksMediaPK pk3 = new XLinksMediaPK(0, "image_3");

        xParserLink.add(new XLinksMedia(pk1, xParserLink));
        xParserLink.add(new XLinksMedia(pk2, xParserLink));
        xParserLink.add(new XLinksMedia(pk3, xParserLink));

        em.persist(xParserLink);

        System.out.println("Success...");

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