hibernate OneToMany composite key

  • Thread starter comp.lang.java.programmer
  • Start date
C

comp.lang.java.programmer

I'm fairly new to hibernate and I am trying to create a Hibernate OneToMany mapping

It's using a composite key (int, int, int) on both parent and child tables.

I have tableA with column (x, y, z, name etc)
Table A contains
1, 2, 3, "tom"

I have tableB with columns (x, y, z, other etc)
Table B contains
1, 2, 3, timestamp, amount
1, 2, 3, timestamp2, amount2
1, 2, 3, timestamp3, amount3


Instead of returning one record from table A (with a set of 3 B records) when I restrict on Restrict.idEq(key)

it is returning the number of records in my "B" table ie
1,2,3,"tom",1, 2, 3, timestamp3, amount3
1,2,3,"tom",1, 2, 3, timestamp3, amount3
1,2,3,"tom",1, 2, 3, timestamp3, amount3
(It seems to only return the most recently added record in B ie timestamp3, amount3)


This is my set up



My embedded class is as follows
@Embeddable
public class MyKey implements Serializable {
private int x;
private int y;
private int z;

The A class is
@Entity
@Table(name="tableA")
public class A implements Serializable {
@Id
private MyKey key;
private String name;

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumns ({
@JoinColumn(name="x", referencedColumnName = "x", insertable=false, updatable=false),
@JoinColumn(name="y", referencedColumnName = "y", insertable=false, updatable=false),
@JoinColumn(name="z", referencedColumnName = "z", insertable=false, updatable=false)
})
public Set<B> set;

The B class is
@Entity
@Table(name="tableB")
public class B implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private MyKey key;
@ManyToOne
@JoinColumns ({
@JoinColumn(name="x", referencedColumnName = "x", insertable=false, updatable=false),
@JoinColumn(name="y", referencedColumnName = "y", insertable=false, updatable=false),
@JoinColumn(name="z", referencedColumnName = "z", insertable=false, updatable=false)
})
public A a; //unidirectional


So long as there is there is just one record in B for every record in A it seems to work.

But when I duplicate (many) records in B (in this case 3 records) and I try to retrieve
I get multiple A records! Each A record has a "set" of B records with just one B record!

Aside:
I started thinking the secondary table B should have a unique primary key by adding in timestamp
But when I did that I got the error: timestamp must be insertable: false, updateable: false
When I added timetamp to the JoinColumns I got the inevitable error: Unable to find logical column: timestamp in table A

So in summary it seems that my set-up is limiting me to one-to-one mapping

Similarly when I attempt to retrieve on B I get the error:
org.hibernate.HibernateException: More than one row with the given identifier was found: MyKey@a5843003, for class: B
 
L

Lew

I'm fairly new to hibernate and I am trying to create a Hibernate OneToMany mapping

It's using a composite key (int, int, int) on both parent and child tables.

I have tableA with column (x, y, z, name etc)
Table A contains
1, 2, 3, "tom"

I have tableB with columns (x, y, z, other etc)
Table B contains
1, 2, 3, timestamp, amount
1, 2, 3, timestamp2, amount2
1, 2, 3, timestamp3, amount3


Instead of returning one record [sic] from table A (with a set of 3 B records [sic]) when I restrict on Restrict.idEq(key)

it is returning the number of records [sic] in my "B" table ie
1,2,3,"tom",1, 2, 3, timestamp3, amount3
1,2,3,"tom",1, 2, 3, timestamp3, amount3
1,2,3,"tom",1, 2, 3, timestamp3, amount3
(It seems to only return the most recently added record in B ie timestamp3, amount3)

Before getting into the Hibernate side, there are issues with the data side,
which, by the way, you explained very incompletely. If I infer something
incorrectly you'll just have to be clearer.

You cannot have a one-to-many relationship between tables with the same key
definition. From what you said, your tables A and B are something similar to
(I use PostgreSQL syntax for an example):

CREATE TABLE "A"
(
x INT NOT NULL,
y INT NOT NULL,
z INT NOT NULL,
aname TEXT,
PRIMARY KEY (x, y, z)
);

CREATE TABLE "B"
(
x INT NOT NULL,
y INT NOT NULL,
z INT NOT NULL,
whilst TIMESTAMP NOT NULL,
amount DECIMAL (10, 2),
PRIMARY KEY (x, y, z)
);

If that structure is in play, then there cannot be three rows in table "B"
with values (1, 2, 3) for (x, y, z). Therefore it cannot be a one-to-many
relationship.

Please clarify.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top