• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA relations become null when flushed

 
Mester Jackob
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a bidirectional relation between entity A and B - when flushed to the database the references in A to B are OK for the first few rows but then become null for the rest of the rows. This is how the bidirectional relation is defined:

@Entity
public class A {
@ManyToOne(cascade = CascadeType.ALL)
private B b;
..
}

@Entity
public class B {
@Id
private String type;

@OneToMany(mappedBy = "b")
private Collection<A> as;
...
}

Instances of both A and B are newly added using persist() (by persisting A which cascade to B) after which I call flush(). I have inspected all instaces of A and B using a debugger and confirmed that the relations are OK in both directions. I haven't shown hashCode() and equals() but they are autogenerated using eclipse which is based on the method described in "effective java" book. The hash and equals of B are only based on the field named "type". The value of "type" is set before persisting the entity and never changed after persisting it.

I tried adding an autogenerated key to B which seem to have solved the problem:

@Entity
public class B {

@SequenceGenerator(name = "b", sequenceName = "b")
@Id
@GeneratedValue(generator = "b")
private long ID;

private String type;
...
}

Now the references in rows of A to B are all OK when flushed to the database.

I would still like to know why the first case of B dosen't work? Is there any fundamental problem in having both the hashcode implementation and the @Id point to the same field??

btw. Im using hibernate 3.3 with hypersonic 1.8.0.7 in a standalone app. Hypersonic has been configured to flush to disk.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic