Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » Entities OneToMany Composite Primary keys
Entities OneToMany Composite Primary keys [message #1702821] Sat, 25 July 2015 04:56 Go to next message
Eclipse UserFriend
Hi all,
I have a problem with these entities/tables:

-------------- TABLES --------------
create table parent (id integer auto_increment, dex varchar(30) not null, primary key (id));
create table children (id_parent integer not null, id integer not null, dex varchar(30) not null, primary key (id_parent, id), foreign key (id_parent) references parent(id) on delete cascade);
create table subchildren (id_parent integer not null, id_child integer not null, id integer not null, dex varchar(30) not null, primary key (id_parent, id_child, id), foreign key (id_parent, id_child) references children(id_parent, id) on delete cascade);

-------------- ENTITIES --------------
@Entity
@Table(name = "parent")
public class Parent {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "parent", orphanRemoval = true)
private List<Child> children;
}

@Entity
@Table(name = "children")
public class Child {
@EmbeddedId
private ChildPk id;
@MapsId
@ManyToOne
@JoinColumn(name = "id_parent")
private Parent parent;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "child", orphanRemoval = true)
private List<SubChild> subchildren;
}

@Embeddable
public class ChildPk {
@Column(name = "id_parent")
private Integer parent;
@Column(name = "id")
private Integer id;
}

@Entity
@Table(name = "subchildren")
public class SubChild {
@EmbeddedId
private SubChildPk id;
@MapsId
@ManyToOne
@JoinColumns({
@JoinColumn(name = "id_parent", referencedColumnName = "id_parent"),
@JoinColumn(name = "id_child", referencedColumnName = "id"),
})
private Child child;
}

@Embeddable
public class SubChildPk {
@EmbeddedId
private ChildPk child;
@Column(name = "id")
private Integer id;
}

The problem is the subchildren table, it persist or merge only the first record in the list.
I searched for a lot but I didn't find anything...
Thank you very much
Bye
Re: Entities OneToMany Composite Primary keys [message #1703244 is a reply to message #1702821] Wed, 29 July 2015 13:14 Go to previous messageGo to next message
Eclipse UserFriend
Have you set the SubChild.id.id value before persisting? This would explain your situation as if it isn't set, there is no way to distinguish between the subchildren and would make them appear to be the same entity.
Re: Entities OneToMany Composite Primary keys [message #1703495 is a reply to message #1703244] Sat, 01 August 2015 05:51 Go to previous messageGo to next message
Eclipse UserFriend
Hi,
yes I set everything before persist and/or merge, I also found that table column id, in subchild pojo, can be misunderstood by persistence layer, so I tried to use @AttributeOverride in either SubChildPk -> child and SubChild -> id but without success.
Now I want to try to change subchild column name, from id to id_subchild and try again.
Re: Entities OneToMany Composite Primary keys [message #1703593 is a reply to message #1702821] Mon, 03 August 2015 09:04 Go to previous messageGo to next message
Eclipse UserFriend
Hi, I made some tests and have news:
I create a parallel project with 3 tables. The persist command on these tables works fine, but the find doesn't correctly load the leaf table data, in fact the SubChildPk -> id attribute has always 1 as value (BUG??? Confused ), and JPA layer returns the same pointer to the object (due the same primary key).
I tried also to rename id field to id_nipote, but with the same results...
I attach Java code so I anyone would like to see...
Thank you very much.
Bye
  • Attachment: jpa.zip
    (Size: 9.88MB, Downloaded 283 times)

[Updated on: Mon, 03 August 2015 09:20] by Moderator

Re: Entities OneToMany Composite Primary keys [message #1703604 is a reply to message #1703593] Mon, 03 August 2015 10:42 Go to previous message
Eclipse UserFriend
Hi all,
switching from @EmbeddedId to @IdClass resolve the problem, and I don't have any issue to change it.
Thank you very much.
Bye
Previous Topic:JAP Batch Writing
Next Topic:[Moxy] json marshalling, mixed element unexpected behaviour
Goto Forum:
  


Current Time: Sun Jun 22 06:02:23 EDT 2025

Powered by FUDForum. Page generated in 0.05464 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top