Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Entities OneToMany Composite Primary keys
Entities OneToMany Composite Primary keys [message #1702821] Sat, 25 July 2015 08:56 Go to next message
Alfredo Marchini is currently offline Alfredo MarchiniFriend
Messages: 4
Registered: July 2015
Junior Member
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 17:14 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
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 09:51 Go to previous messageGo to next message
Alfredo Marchini is currently offline Alfredo MarchiniFriend
Messages: 4
Registered: July 2015
Junior Member
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 13:04 Go to previous messageGo to next message
Alfredo Marchini is currently offline Alfredo MarchiniFriend
Messages: 4
Registered: July 2015
Junior Member
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 271 times)

[Updated on: Mon, 03 August 2015 13:20]

Report message to a moderator

Re: Entities OneToMany Composite Primary keys [message #1703604 is a reply to message #1703593] Mon, 03 August 2015 14:42 Go to previous message
Alfredo Marchini is currently offline Alfredo MarchiniFriend
Messages: 4
Registered: July 2015
Junior Member
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: Thu Dec 26 23:38:00 GMT 2024

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

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

Back to the top