[jboss-user] [EJB 3.0] - Re: Etity reference problems

oskar.carlstedt do-not-reply at jboss.com
Tue Apr 3 03:15:15 EDT 2007


Thank you for helping me!

I found the error I did. In my code where I assigned all objects I missed to add the B item to the list of B:s in A. Doing this resolved my problem. The following steps must be applied:

1. Create A
2. Create all B:s and add them to A
3. Create C by using A and the expected B

I missed to add the B, that was mapped from A and C, to A. Kind of stupidness from  my side. I put my working code here for all of you who are interested in this:


  | @Entity
  | @Table(name = "A")
  | public class A implements Serializable {
  | 
  | 	@Id
  | 	@GeneratedValue(strategy = GenerationType.IDENTITY)
  | 	@Column(name = "Aid")
  | 	private Integer id;
  | 
  | 	@OneToOne(cascade = CascadeType.ALL, mappedBy = "a", fetch = FetchType.EAGER)
  | 	private C c;
  | 
  | 	@OneToMany(cascade = CascadeType.ALL, mappedBy = "a", fetch = FetchType.EAGER)
  | 	private List<B> bs = new ArrayList<B>();
  | 	
  | 	...
  | 
  | 	protected A() {};
  | 
  | 	public A(...) {
  | 		...
  | 	}
  | 
  | 	public List<B> getBs() {
  | 		return bs;
  | 	}
  | 
  | 	public void setBs(List<B> bs) {
  | 		this.bs = bs;
  | 	}
  | 
  | 	public void addB(B b) {
  | 
  | 		b.setA(this);
  | 		this.bs.add(b);
  | 	}
  | 
  | 	public C getC() {
  | 		return c;
  | 	}
  | 
  | 	public void setC(C c) {
  | 		c.setA(this);
  | 		this.c = c;
  | 	}
  | 	
  | 	...
  | }
  | 
  | 
  | 
  | @Entity
  | @Table(name = "B")
  | public class B implements Serializable {
  | 
  | 	@Id
  | 	@GeneratedValue(strategy = GenerationType.IDENTITY)
  | 	@Column(name = "Bid")
  | 	private Integer id;
  | 
  | 	@ManyToOne
  | 	@JoinColumn(name = "Aid", nullable = false)
  | 	private A a;
  | 	
  | 	...
  | 	
  | 	protected B() {};
  | 
  | 	public B(A a, ...) {
  | 
  | 		this.a = a;
  | 		...
  | 	}
  | 
  | 	public Integer getId() {
  | 		return id;
  | 	}
  | 
  | 	public void setId(Integer id) {
  | 		this.id = id;
  | 	}
  | 
  | 	public A getA() {
  | 		return a;
  | 	}
  | 
  | 	public void setA(A a) {
  | 		this.a = a;
  | 	}
  | 
  | 	...
  | }
  | 
  | 
  | 
  | @Entity
  | @Table(name = "C")
  | public class C implements Serializable {
  | 
  | 	@Id
  | 	@GeneratedValue(strategy = GenerationType.IDENTITY)
  | 	@Column(name = "Cid")
  | 	private Integer id;
  | 
  | 	@OneToOne
  | 	@JoinColumn(name = "Aid", nullable = false)
  | 	private A a;
  | 
  | 	@OneToOne
  | 	@JoinColumn(name = "Bid", nullable = false)
  | 	private B b;
  | 
  | 	
  | 	protected C() {};
  | 
  | 	public C(A a,
  | 			B b) {
  | 
  | 		this.a = a;
  | 		this.b = b;
  | 	}
  | 
  | 	public Integer getId() {
  | 		return id;
  | 	}
  | 
  | 	public void setId(Integer id) {
  | 		this.id = id;
  | 	}
  | 
  | 	public A getA() {
  | 		return a;
  | 	}
  | 
  | 	public void setA(A a) {
  | 		this.a = a;
  | 	}
  | 
  | 	public B getB() {
  | 		return b;
  | 	}
  | 
  | 	public void setB(B b) {
  | 		this.b = b;
  | 	}
  | }
  | 
  | 



Finally some points:
- There is an error in my first post. "... mappedBy="A"..." shall be "... mappedBy="a"...". My mistake in the post.

- This fix requires the C table to carry its own primary key, a Cid.

- As you say Felix, it doesn't look good to point out Aid twice. This shall work, but it seems to be a bug in hibernate (see http://forum.hibernate.org/viewtopic.php?t=970823). JBoss 4.0.5.GA is using Hibernate 3.2, it shall be fixed in Hibernate 3.3.0. I haven't tried the join table, I will check it out and se what more it gives to me. It might be a kind of shortcut for my C table mapping.

Thanks again for quick response.

Best
Oskar


 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4033902#4033902

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4033902



More information about the jboss-user mailing list