gpt4 book ai didi

java - JPA OneToMany 和 ManyToOne 之间的关系

转载 作者:行者123 更新时间:2023-12-01 05:03:17 26 4
gpt4 key购买 nike

我是 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;

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

这是我尝试在 XParseLinks 类中设置 fotos 变量的方法(preke:是 XParseLinks 对象)他的主键 LinkId):

XLinksMedia foto = new XLinksMedia();
foto.setXLinksMediaPK(new XLinksMediaPK());

foto.setxParserLink(preke);
foto.getXLinksMediaPK().setImage(e.attr("src"));

preke.getFotos().add(foto);

最佳答案

即使我不知道你的期望是什么,当我测试你的编码时,它是好的。我只是在您的类中放置了一些OverLoad Constructor,以便轻松进行数据初始化。但是,您使用 GenerationType.IDENTITY,这就是我为 XParserLinkslink_id 设置自动创建列的原因。以下代码的主要内容与您的相同。

您的观点这是我尝试在 XParseLinks 类中设置 fotos 变量的方法(preke:是 XParseLinks 对象,它有他的主键 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);
}
}

测试.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();
}
}

关于java - JPA OneToMany 和 ManyToOne 之间的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13064566/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com