jpa - One-To-One unidirectional relationship -
I have two institutions, ap and apatatus, one-by-one-one-one-one-one-one- In one relationship Only AP should be able to reach epistatus. Epistatus needs to identify only AP, which will also serve as the primary key for epistatus. Basically, Epistatus is like an embedded object, but I want a separate table for it. Here's what I have:
My entities
@Entity public class AP {@Id @GeneratedValue (strategy = GenerationType.AUTO) @ column (name = "id" , Nullable = false) Private Ent ID; @OneToOne Private Epistatus Epitatus; // Gates and Setters} Entity Public Class Epistas {@Id @Column (name = "AP_ID", nullable = false) Private Ent APID; // gates and satellers} my test
public class aparipositoryte {@Autowired APRepository repository; @Autowired APStatusRepository Status Repository; @Test Public Zero Test () {AP AP = new AP (); Apestatus Status = New Epistatus (); StatusRepository.save (status); Ap.setApStatus (status); Repository.save (ap); Status.setApId (ap.getId ()); AP DBAP = Repository Fund On (APIGID); AssertNotNull (dbAp); AssertNotNull (dbAp.getApStatus ()); AssertEquals (dbAp.getId (), dbAp.getApStatus (). GetApId ()); }} emphasis failures fail, expected: & lt; 1 & gt; But & lt; 0 & gt; Was there. And I already know that why I am setting the APID field of the situation after the situation and the AP have already been saved. But before saving it the problem is that I would like to set the area equal to zero, since the execution of repository.save (AP) , the ID of the API is generated for a new value ( The case in this one). I have already experimented with making connections and adding effects, but so far I have failed. Can someone tell me in the right direction or tell me how can I fix this? Thanks in advance. EDIT: For now, to make the relationships bidirectional and the recipient method in the epititus class resembles the following. If anyone has a better answer then please share
public int getApId () {return ap.getId (); }
First of all, you can not change an institution ID. It is set once and only once to identify the unit, after which, you are forcing this example to be completely different, which is not supported in JPA.
This means that if status.setApId call changes the apid field, then this is only the actual value.
Second, I did not quite understand the APTATUS reference of AP. Is your AP a foreign key for Apestats, or do you intend to use this AP.ID APStatus.AP_ID relationship? My guess is that the purpose of this is to reuse APID apestats.AP_ID resins, because it sounds more common, but how it affects only you.
You can work on it without first changing your mapping, saving AP, using your identity in apatatus and saving, then updating the relationship:
AP AP = new AP (); Apestatus Status = New Epistatus (); Repository.save (ap); Status.setApId (ap.getId ()); StatusRepository.save (status); Ap.setApStatus (status); Repository.save (ap); Or you can replace your mapping with the derivative of JPA 2.0, set cascade settings, and then only save AP or Epistas: < Pre> @Entity Public Class AP {@Id @GeneratedValue (strategy = GenerationType.AUTO) @Column (name = "ID", nullable = false) Private ID; @OneToOne (mapped = "ap", cascade = cascade type .all, orphan removals = true) private epistatus epitatus; // gates and setters} entity public class epistas {@IdToOne @JoinColumn (name = "AP_ID") private AP AP; // gates and sets}} Then you can just use it:
ap ap = new ap (); Apestatus Status = New Epistatus (); Status.setAp (AP); Ap.setApStatus (status); Repository.save (ap);
Comments
Post a Comment