我有一个问题,关于在数据库(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..