OneToOne, Composite Primary Key, Foreign Key

View: New views
5 Messages — Rating Filter:   Alert me  

OneToOne, Composite Primary Key, Foreign Key

by Max Müller-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everybody.

I have problem with OneToOne-Mappings. The related entities share one field of the primary key. This field ist also a foreign key. So, it looks like:

Address
-------
company        PK, FK
address        PK
person             FK
street
city


Person
------
company        PK
person         PK
firstname
lastname


The tables are currently mapped in following way:

public class Address implements Serializable
{
    public static final long serialVersionUID = 2L;

    @Id
    @Column(name = "address")
    private int address;

    @Id
    @Column(name = "company")
    private int company;

    @Column(name = "street")
    private String street;

    @Column(name = "city")
    private String city;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumns({
        @JoinColumn(name="person", referencedColumnName="person"),
        @JoinColumn(name="company", referencedColumnName="company",
            insertable=false, nullable=false, updatable=false)
    })
    private Person person;

    // getters and setters
}

@Entity
@Table(name = "person")
@IdClass(PersonPk.class)
public class Person implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "person")
    private int person;

    @Id
    @Column(name = "company")
    private int company;

    @Column(name = "firstname")
    private String firstname;

    @Column(name = "lastname")
    private String lastname;

    @OneToOne(mappedBy="person", cascade=CascadeType.ALL)
    private Address address;

    // getters and setters
}

Reading data works without errors. But, e.g. if I want to update or insert an Address entity, all fields will be updated in database except the person column. What's wrong with my mapping?

Thanks for helping!

Max



_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: OneToOne, Composite Primary Key, Foreign Key

by christopher delahunt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Max,

I believe you are hitting bug
https://bugs.eclipse.org/bugs/show_bug.cgi?id=280436
Can you try making Address' company attribute updateable=false,
insertable=false, and the OneToOne updateable=true, insertable=true.  
Or you can add a new basic mapping for the person FK that is writable.

Best Regards,
Chris

Max Müller wrote:

> Hi everybody.
>
> I have problem with OneToOne-Mappings. The related entities share one
> field of the primary key. This field ist also a foreign key. So, it
> looks like:
>
> Address
> -------
> company        PK, FK
> address        PK
> person             FK
> street
> city
>
>
> Person
> ------
> company        PK
> person         PK
> firstname
> lastname
>
>
> The tables are currently mapped in following way:
>
> public class Address implements Serializable
> {
>     public static final long serialVersionUID = 2L;
>
>     @Id
>     @Column(name = "address")
>     private int address;
>
>     @Id
>     @Column(name = "company")
>     private int company;
>
>     @Column(name = "street")
>     private String street;
>
>     @Column(name = "city")
>     private String city;
>
>     @OneToOne(cascade=CascadeType.ALL)
>     @JoinColumns({
>         @JoinColumn(name="person", referencedColumnName="person"),
>         @JoinColumn(name="company", referencedColumnName="company",
>             insertable=false, nullable=false, updatable=false)
>     })
>     private Person person;
>
>     // getters and setters
> }
>
> @Entity
> @Table(name = "person")
> @IdClass(PersonPk.class)
> public class Person implements Serializable
> {
>     private static final long serialVersionUID = 1L;
>
>     @Id
>     @GeneratedValue(strategy = GenerationType.AUTO)
>     @Column(name = "person")
>     private int person;
>
>     @Id
>     @Column(name = "company")
>     private int company;
>
>     @Column(name = "firstname")
>     private String firstname;
>
>     @Column(name = "lastname")
>     private String lastname;
>
>     @OneToOne(mappedBy="person", cascade=CascadeType.ALL)
>     private Address address;
>
>     // getters and setters
> }
>
> Reading data works without errors. But, e.g. if I want to update or
> insert an Address entity, all fields will be updated in database
> except the person column. What's wrong with my mapping?
>
> Thanks for helping!
>
> Max
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@...
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>  
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: OneToOne, Composite Primary Key, Foreign Key

by Max Müller-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Chris,

thanks for you help. Your second suggestion works, but I don't really like it. Do you nearly know when the bug will be fixed?

Best Regards,
Max



2009/8/14 christopher delahunt <christopher.delahunt@...>
Hello Max,

I believe you are hitting bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=280436
Can you try making Address' company attribute updateable=false, insertable=false, and the OneToOne updateable=true, insertable=true.   Or you can add a new basic mapping for the person FK that is writable.

Best Regards,
Chris

Max Müller wrote:
Hi everybody.

I have problem with OneToOne-Mappings. The related entities share one field of the primary key. This field ist also a foreign key. So, it looks like:

Address
-------
company        PK, FK
address        PK
person             FK
street
city


Person
------
company        PK
person         PK
firstname
lastname


The tables are currently mapped in following way:

public class Address implements Serializable
{
   public static final long serialVersionUID = 2L;

   @Id
   @Column(name = "address")
   private int address;

   @Id
   @Column(name = "company")
   private int company;

   @Column(name = "street")
   private String street;

   @Column(name = "city")
   private String city;

   @OneToOne(cascade=CascadeType.ALL)
   @JoinColumns({
       @JoinColumn(name="person", referencedColumnName="person"),
       @JoinColumn(name="company", referencedColumnName="company",
           insertable=false, nullable=false, updatable=false)
   })
   private Person person;

   // getters and setters
}

@Entity
@Table(name = "person")
@IdClass(PersonPk.class)
public class Person implements Serializable
{
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "person")
   private int person;

   @Id
   @Column(name = "company")
   private int company;

   @Column(name = "firstname")
   private String firstname;

   @Column(name = "lastname")
   private String lastname;

   @OneToOne(mappedBy="person", cascade=CascadeType.ALL)
   private Address address;

   // getters and setters
}

Reading data works without errors. But, e.g. if I want to update or insert an Address entity, all fields will be updated in database except the person column. What's wrong with my mapping?

Thanks for helping!

Max


------------------------------------------------------------------------

_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
 
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users


_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: OneToOne, Composite Primary Key, Foreign Key

by Max Müller-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everybody.

I have another question in this case.

If I want to merge a new transient Person that has an detached Adress, the transient Person will be inserted into database, but the Adress object will not be updated in database. With OpenJPA it works.

Is this another bug in EclipseLink?

Thank you very much!

Best Regards,
Max



2009/8/17 Max Müller <max.mueller.max@...>
Hi Chris,

thanks for you help. Your second suggestion works, but I don't really like it. Do you nearly know when the bug will be fixed?

Best Regards,
Max



2009/8/14 christopher delahunt <christopher.delahunt@...>

Hello Max,

I believe you are hitting bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=280436
Can you try making Address' company attribute updateable=false, insertable=false, and the OneToOne updateable=true, insertable=true.   Or you can add a new basic mapping for the person FK that is writable.

Best Regards,
Chris

Max Müller wrote:
Hi everybody.

I have problem with OneToOne-Mappings. The related entities share one field of the primary key. This field ist also a foreign key. So, it looks like:

Address
-------
company        PK, FK
address        PK
person             FK
street
city


Person
------
company        PK
person         PK
firstname
lastname


The tables are currently mapped in following way:

public class Address implements Serializable
{
   public static final long serialVersionUID = 2L;

   @Id
   @Column(name = "address")
   private int address;

   @Id
   @Column(name = "company")
   private int company;

   @Column(name = "street")
   private String street;

   @Column(name = "city")
   private String city;

   @OneToOne(cascade=CascadeType.ALL)
   @JoinColumns({
       @JoinColumn(name="person", referencedColumnName="person"),
       @JoinColumn(name="company", referencedColumnName="company",
           insertable=false, nullable=false, updatable=false)
   })
   private Person person;

   // getters and setters
}

@Entity
@Table(name = "person")
@IdClass(PersonPk.class)
public class Person implements Serializable
{
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "person")
   private int person;

   @Id
   @Column(name = "company")
   private int company;

   @Column(name = "firstname")
   private String firstname;

   @Column(name = "lastname")
   private String lastname;

   @OneToOne(mappedBy="person", cascade=CascadeType.ALL)
   private Address address;

   // getters and setters
}

Reading data works without errors. But, e.g. if I want to update or insert an Address entity, all fields will be updated in database except the person column. What's wrong with my mapping?

Thanks for helping!

Max


------------------------------------------------------------------------

_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users
 
_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users



_______________________________________________
eclipselink-users mailing list
eclipselink-users@...
https://dev.eclipse.org/mailman/listinfo/eclipselink-users

Re: OneToOne, Composite Primary Key, Foreign Key

by James Sutherland :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Merge will only merge changes to objects, if the Address did not change, it will not be committed.  Also if the relationship is not marked cascade merge, then the merge of the Person will not cascade to the Address.

Perhaps include the details of what you are doing and what you expect to occur.

Max Müller-2 wrote:
Hi everybody.

I have another question in this case.

If I want to merge a new transient Person that has an detached Adress, the
transient Person will be inserted into database, but the Adress object will
not be updated in database. With OpenJPA it works.

Is this another bug in EclipseLink?

Thank you very much!

Best Regards,
Max



2009/8/17 Max Müller <max.mueller.max@googlemail.com>

> Hi Chris,
>
> thanks for you help. Your second suggestion works, but I don't really like
> it. Do you nearly know when the bug will be fixed?
>
> Best Regards,
> Max
>
>
>
> 2009/8/14 christopher delahunt <christopher.delahunt@oracle.com>
>
>  Hello Max,
>>
>> I believe you are hitting bug
>> https://bugs.eclipse.org/bugs/show_bug.cgi?id=280436
>> Can you try making Address' company attribute updateable=false,
>> insertable=false, and the OneToOne updateable=true, insertable=true.   Or
>> you can add a new basic mapping for the person FK that is writable.
>>
>> Best Regards,
>> Chris
>>
>> Max Müller wrote:
>>
>>> Hi everybody.
>>>
>>> I have problem with OneToOne-Mappings. The related entities share one
>>> field of the primary key. This field ist also a foreign key. So, it looks
>>> like:
>>>
>>> Address
>>> -------
>>> company        PK, FK
>>> address        PK
>>> person             FK
>>> street
>>> city
>>>
>>>
>>> Person
>>> ------
>>> company        PK
>>> person         PK
>>> firstname
>>> lastname
>>>
>>>
>>> The tables are currently mapped in following way:
>>>
>>> public class Address implements Serializable
>>> {
>>>    public static final long serialVersionUID = 2L;
>>>
>>>    @Id
>>>    @Column(name = "address")
>>>    private int address;
>>>
>>>    @Id
>>>    @Column(name = "company")
>>>    private int company;
>>>
>>>    @Column(name = "street")
>>>    private String street;
>>>
>>>    @Column(name = "city")
>>>    private String city;
>>>
>>>    @OneToOne(cascade=CascadeType.ALL)
>>>    @JoinColumns({
>>>        @JoinColumn(name="person", referencedColumnName="person"),
>>>        @JoinColumn(name="company", referencedColumnName="company",
>>>            insertable=false, nullable=false, updatable=false)
>>>    })
>>>    private Person person;
>>>
>>>    // getters and setters
>>> }
>>>
>>> @Entity
>>> @Table(name = "person")
>>> @IdClass(PersonPk.class)
>>> public class Person implements Serializable
>>> {
>>>    private static final long serialVersionUID = 1L;
>>>
>>>    @Id
>>>    @GeneratedValue(strategy = GenerationType.AUTO)
>>>    @Column(name = "person")
>>>    private int person;
>>>
>>>    @Id
>>>    @Column(name = "company")
>>>    private int company;
>>>
>>>    @Column(name = "firstname")
>>>    private String firstname;
>>>
>>>    @Column(name = "lastname")
>>>    private String lastname;
>>>
>>>    @OneToOne(mappedBy="person", cascade=CascadeType.ALL)
>>>    private Address address;
>>>
>>>    // getters and setters
>>> }
>>>
>>> Reading data works without errors. But, e.g. if I want to update or
>>> insert an Address entity, all fields will be updated in database except the
>>> person column. What's wrong with my mapping?
>>>
>>> Thanks for helping!
>>>
>>> Max
>>>