mysql插入整个实体_在数据库上插入实体外键的正确方法(使用:Mysql,JPA)

我有一个问题,关于在数据库(Mysql,jpa)上插入我的实体外键的正确方法 . 在问我的问题之前,我尝试了我找到的解决方案,而且我花了好几天时间自行解决问题 . 我相信我有什么不对劲:

我需要做的是:

1. I have already data (informations) exists in Site table

2. I have to make the insertion of new sequence in Sequence table

3. I have to make the insertion of new sequence in Sequence table, and association of FK site's (site exists and no need to inserting it again)

主要问题是:

1. when I insert in Sequence Table I have Sequence with FK of Site inserted correctelly

. BUT ! the problem is I have also Site insertion in database, so redundant insertion of Site evry time I insert Sequence datas

我需要插入一个具有关联的序列(Site的FK) .

在我的控制台上发生的事情:

Hibernate: insert into Sites (Current_Sequence, Date_Insert, Date_Update, Enable, Nom_Site, User_Update, applicationService_Id_SA, partner_Id_Partner, Id_Site) values (?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: insert into Sequences (Date_Insert_timestamp, Date_Update_timestamp, Nbr_PDF, Nbr_ZPL, Size_PDF, Size_ZPL, TimestampMinute, User_Update, site_Id_Site, status, Id_Sequence) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate: insert into Sites_Sequences (Site_Id_Site, sequences_Id_Sequence) values (?, ?)

Jpa存储库:我首先按名称获取Site对象

@Override

public Site findSiteByName(String Name_Site) {

List sites = entityManager.createQuery("SELECT s FROM Site s").getResultList();

for (Site item : sites) {

if (item.getNom_Site().equals(Name_Site)) {

return item;

}

}

return null;

}

Jpa方法在数据库上插入Sequence

@Override

public Sequence saveSequence(Sequence sequence) {

return super.save(sequence);

}

我的休息控制器:

@GET

@Path("/addSeq")

@Produces(MediaType.APPLICATION_JSON)

@Consumes(MediaType.APPLICATION_JSON)

public Sequence addSeq(@Context final UriInfo uriInfo) {

Site site = siteService.findSiteByName("Name_Site");

Sequence sequence = sequenceFactory.CreateSequence(new Date(), new Date(), "", "", "", 4, 6, 10, 12);

sequence.setSite(site);

site.getSequences().add(sequence);

sequenceservice.saveSequence(sequence);

return null;

}

我的主要课程是:

@Entity

@Table(name = "Sequences")

public class Sequence {

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

@Column(name = "Id_Sequence", unique = true, nullable = false)

private long Id_Sequence;

private Date ....;

private Date ....;

private String ....;

private String ....;

private String ....;

private int ....;

private int ....;

private double ....;

private double ....;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)

private Set traceSequences = new HashSet();

@ManyToOne(cascade = CascadeType.ALL)

private Site site;

public constructor...

public set..

public get..

@Entity

@Table(name = "Sites")

public class Site {

@Id

@GeneratedValue(strategy = GenerationType.AUTO)

@Column(name = "Id_Site", unique = true, nullable = false)

private long Id_Site;

private String ...;

private boolean ...;

private long ...;

private Date ...;

private Date ...;

private String ...;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)

private Set sequences = new HashSet();

@ManyToOne

private ... ...;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)

private Set ... = new HashSet();

@ManyToOne

private ... ...;

public constructor...

public set..

public get..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值