Etity reference problems
oskar.carlstedt Mar 30, 2007 4:48 AMHi All!
I have a quite strange data model, but it has it explanation. Lets say we have the following tables in my db:
----------- | A | ----------- | Aid (pk)| | | ----------- ----------- | B | ----------- | Bid (pk)| | Aid (fk)| | | ----------- ----------- | C | ----------- | Aid (pk)| | Bid (fk)| | | ----------- There is a unique constraint on the tuple (Aid, Bid)
A textual explanation of the model:
A is the master table. It has more info than the columns I described. But the most important one is the primary key Aid.
B is another table. Items in B references A, i.e. there are many B items on one A item (many-to-one).
C is a table that will point out one row in B from A. So there is a one-to-one relationship between A and B that is stored in table C.
The problem here is that we have a kind of circular reference. Is it possible to model this in EJB3 with annotation. I have tried a lot, but nothing works. I often get a NPE for OneToOne on line 135.
The classesre defined as follows:
public class A { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "Aid") private Integer id; @OneToMany(cascade = CascadeType.ALL, mappedBy = "A", fetch = FetchType.LAZY) private List<B> Bs = new ArrayList<B>(); @OneToOne @PrimaryKeyJoinColumn private C c = null; ... } public class B { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "Bid") private Integer Bid; @ManyToOne @JoinColumn(name = "Aid", nullable = false) private A a; ... } public class C { @Id @Column(name = "Aid") private Integer id; @OneToOne @JoinColumn(name = "Aid", nullable=false) private A a; @OneToOne @PrimaryKeyJoinColumn private B b; ... }
So, it is a problem doing this way. Does anyone know how to get it to work. I know the datamodel is a bit tricky - the explanation I have is that we want all references to A be in the same direction.
Environment:
JBoss 4.0.5.GA
JBoss EJB3 RC9 Patch
JBoss WS 1.2.0GA SP1
Database MySQL 5, no InnoDB
I would really appreciate if someone can help me
Best Regards
Oskar