Skip to content

Instantly share code, notes, and snippets.

@jlogar
Last active August 24, 2017 13:39
Show Gist options
  • Save jlogar/2da2237640aa013f2cfbda33a4a5dc84 to your computer and use it in GitHub Desktop.
Save jlogar/2da2237640aa013f2cfbda33a4a5dc84 to your computer and use it in GitHub Desktop.
Mapping to a child table twice - @OneToMany & @manytoone
@Entity
@Table(name = "child")
public class Child {
@ManyToOne(optional = false)
private Parent parent;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Deprecated
protected Child() {
}
Child(Parent parent) {
this.parent = parent;
}
public int getId() {
return id;
}
}
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.username">postgres</property>
<property name="connection.password">postgres</property>
<property name="connection.url">jdbc:postgresql://localhost/test</property>
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>
@Entity
@Table(name="parent")
class Parent {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@OneToOne(optional = false, cascade = CascadeType.PERSIST)
@JoinColumn(name = "current_child_id")
private Child currentChild;
@OneToMany(mappedBy = "parent")
private List<Child> childHistory;
@Deprecated
protected Parent() {}
Parent(String test) {
this.currentChild = new Child(this);
childHistory = new ArrayList<>();
childHistory.add(this.currentChild);
}
int getId() {
return id;
}
Child getCurrentChild() {
return currentChild;
}
List<Child> getChildHistory() {
return childHistory;
}
}
public class TestApp {
public static void main(String[] args) throws ClassNotFoundException {
Class.forName("org.postgresql.Driver");
final Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
cfg.addAnnotatedClass(Parent.class);
cfg.addAnnotatedClass(Child.class);
try (SessionFactory factory = cfg.buildSessionFactory()) {
final int parentId = persistParent(factory);
try (Session session1 = factory.openSession()) {
Transaction t1 = session1.beginTransaction();
Parent parent1 = session1.find(Parent.class, parentId);
Assert.assertEquals(1, parent1.getChildHistory().size());
Assert.assertEquals(parent1.getChildHistory().get(0).getId(), parent1.getCurrentChild().getId());
t1.commit();
session1.close();
}
}
}
private static int persistParent(SessionFactory factory) {
int parentId;
try (Session session = factory.openSession()) {
final Transaction t = session.beginTransaction();
final Parent parent = new Parent("123");
session.persist(parent);
t.commit();
session.close();
parentId = parent.getId();
System.out.println("p: " + parentId); // Display the string.
}
return parentId;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment