Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Eclipselink support for one-way ManyToOne relationships?

You mentions save/merge - which is it?  

You should use em.persist(p); em.flush(); so that the primary key gets set from the IDENTITY in the DB. 
 If you are using merge, you must pass back and use the reference returned from the em.merge call or it will be treated as an unmanaged instance, which is why you are getting a separate insert on each call.



> On Apr 2, 2019, at 11:17 AM, Glenn D'Mello <glenn@xxxxxxxxxx> wrote:
> 
> I'm seeing something strange with this configuration:
> 
> Two entities:
> 
> @Entity
> public class Person {
>     @Id
>     @GeneratedValue(strategy = GenerationType.IDENTITY)
>     @Column(name="person_id")
>     private Long id;
> 
>     private String name;
>     //...
> }
> 
> @Entity
> public class Phone {
>     @Id
>     @GeneratedValue(strategy = GenerationType.IDENTITY)
>     @Column(name = "phone_id")
>     private Long phoneId;
> 
>     private String number;
> 
>     @ManyToOne(cascade = CascadeType.ALL, optional=false)
>     @JoinColumn(name = "person_id")
>     private Person person;
> }
> 
> 
> And using them with:
> 
> Person p = new Person();
> p.setName("A Person");
> 
> Phone ph1 = new Phone();
> ph1.setNumber("212-1111");
> ph1.setPerson(p);
> 
> Phone ph2 = new Phone();
> ph2.setNumber("313-4444");
> ph2.setPerson(p);
> 
> generates the following sql:
> 
> INSERT INTO PERSON (NAME) VALUES (?)
> bind => [A Person]
> INSERT INTO PHONE (NUMBER, PERSON_person_id) VALUES (?, ?)
> bind => [212-1111, 1]
> 
> INSERT INTO PERSON (NAME) VALUES (?)
> bind => [A Person]
> INSERT INTO PHONE (NUMBER, PERSON_person_id) VALUES (?, ?)
> bind => [313-4444, 2]
> 
> 
> I tried to save/merge the Person right after creation, but that only gave me an extra insert into the person table:
> 
> INSERT INTO PERSON (NAME) VALUES (?)
> bind => [A Person]
> 
> INSERT INTO PERSON (NAME) VALUES (?)
> bind => [A Person]
> INSERT INTO PHONE (NUMBER, PERSON_person_id) VALUES (?, ?)
> bind => [212-1111, 2]
> 
> INSERT INTO PERSON (NAME) VALUES (?)
> bind => [A Person]
> INSERT INTO PHONE (NUMBER, PERSON_person_id) VALUES (?, ?)
> bind => [313-4444, 3]
> 
> When I debug and inspect the objects, I can see that the phone object has the _persistence_* woven fields, but the person object does not. Is there a way to force eclipse to weave that class? 
> 
> 
> 
> Am I missing something in my declarations? Does Eclipselink not support this style of relationships? I tried the same thing with hibernate, and it does the right thing.
> Do I need some other annotation? 
> 
> Using spring-boot 2.1.3, eclipselink 2.7.4, runtime-weaving.
> 
> 
> Thanks,
> Glenn
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or unsubscribe from this list, visit
> https://urldefense.proofpoint.com/v2/url?u=https-3A__www.eclipse.org_mailman_listinfo_eclipselink-2Dusers&d=DwICAg&c=RoP1YumCXCgaWHvlZYR8PZh8Bv7qIrMUB65eapI_JnE&r=aeYmvFwRsCWfafng1rj-AcoD-cr8mkJQBEoVD5OszU0&m=oB3a5DwloCG45u67hyuGXOPMd3WTxW5ACILFbl6ZOK8&s=u58EfeAPEynik_cTW77aQEc2MIrRQXE6jxiO9Guzdws&e=



Back to the top