Created
September 2, 2011 13:03
-
-
Save tomwhoiscontrary/1188549 to your computer and use it in GitHub Desktop.
An example to support my answer on StackOverflow: http://stackoverflow.com/questions/7196458/ejb-3-entities-multiple-columns-same-object-reference-problem/7197660#7197660
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.stackoverflow.jaizen; | |
import javax.persistence.Column; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
import javax.persistence.ManyToOne; | |
@Entity | |
public class A { | |
@Id | |
private int id; | |
private String name; | |
@ManyToOne | |
@Column(name = "manager_id") | |
private B manager; | |
@ManyToOne | |
@Column(name = "supervisor_id") | |
private B supervisor; | |
@ManyToOne | |
@Column(name = "worker_id") | |
private B worker; | |
public A(int id, String name) { | |
this.id = id; | |
this.name = name; | |
} | |
protected A() {} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public B getManager() { | |
return manager; | |
} | |
public void setManager(B manager) { | |
this.manager = manager; | |
} | |
public B getSupervisor() { | |
return supervisor; | |
} | |
public void setSupervisor(B supervisor) { | |
this.supervisor = supervisor; | |
} | |
public B getWorker() { | |
return worker; | |
} | |
public void setWorker(B worker) { | |
this.worker = worker; | |
} | |
public int getId() { | |
return id; | |
} | |
@Override | |
public int hashCode() { | |
return id; | |
} | |
@Override | |
public boolean equals(Object that) { | |
return (this == that) || ((that instanceof A) && (this.id == ((A) that).id)); | |
} | |
@Override | |
public String toString() { | |
return "A [id=" + id + ", name=" + name + ", manager=" + manager + ", supervisor=" + supervisor + ", worker=" + worker + "]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.stackoverflow.jaizen; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
@Entity | |
public class B { | |
@Id | |
private int id; | |
private String name; | |
public B(int id, String name) { | |
this.id = id; | |
this.name = name; | |
} | |
protected B() {} | |
public int getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public int hashCode() { | |
return id; | |
} | |
@Override | |
public boolean equals(Object that) { | |
return (this == that) || ((that instanceof B) && (this.id == ((B) that).id)); | |
} | |
@Override | |
public String toString() { | |
return "B [id=" + id + ", name=" + name + "]"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.stackoverflow.jaizen; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
public class JaizenQuestion { | |
private static final int CARMEN_ID = 300; | |
private static final int BOB_ID = 200; | |
private static final int ALICE_ID = 100; | |
private static final int JOB_ID = 9000; | |
public static void main(String[] args) { | |
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sandbox"); | |
setup(emf); | |
change(emf); | |
System.out.println("------------------------------------------------------------------------------------------"); | |
query(emf); | |
emf.close(); | |
} | |
private static void setup(EntityManagerFactory emf) { | |
EntityManager em = emf.createEntityManager(); | |
em.getTransaction().begin(); | |
B alice = em.merge(new B(ALICE_ID, "Alice")); | |
B bob = em.merge(new B(BOB_ID, "Bob")); | |
B carmen = em.merge(new B(CARMEN_ID, "Carmen")); | |
System.out.println(alice); | |
System.out.println(bob); | |
System.out.println(carmen); | |
A job = em.merge(new A(JOB_ID, "The Job")); | |
job.setManager(alice); | |
job.setSupervisor(alice); | |
job.setWorker(alice); | |
System.out.println(job); | |
em.getTransaction().commit(); | |
em.close(); | |
} | |
private static void change(EntityManagerFactory emf) { | |
EntityManager em = emf.createEntityManager(); | |
em.getTransaction().begin(); | |
A job = em.find(A.class, JOB_ID); | |
B bob = em.find(B.class, BOB_ID); | |
B carmen = em.find(B.class, CARMEN_ID); | |
job.setSupervisor(bob); | |
job.setWorker(carmen); | |
em.getTransaction().commit(); | |
em.close(); | |
} | |
private static void query(EntityManagerFactory emf) { | |
EntityManager em = emf.createEntityManager(); | |
em.getTransaction().begin(); | |
B alice = em.find(B.class, ALICE_ID); | |
B bob = em.find(B.class, BOB_ID); | |
B carmen = em.find(B.class, CARMEN_ID); | |
System.out.println(alice); | |
System.out.println(bob); | |
System.out.println(carmen); | |
A job = em.find(A.class, JOB_ID); | |
System.out.println(job); | |
em.getTransaction().commit(); | |
em.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
B [id=100, name=Alice] | |
B [id=200, name=Bob] | |
B [id=300, name=Carmen] | |
A [id=9000, name=The Job, manager=B [id=100, name=Alice], supervisor=B [id=100, name=Alice], worker=B [id=100, name=Alice]] | |
------------------------------------------------------------------------------------------ | |
B [id=100, name=Alice] | |
B [id=200, name=Bob] | |
B [id=300, name=Carmen] | |
A [id=9000, name=The Job, manager=B [id=100, name=Alice], supervisor=B [id=200, name=Bob], worker=B [id=300, name=Carmen]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment