one to one done

This commit is contained in:
eugenp 2013-05-18 17:50:40 +03:00
parent c1dce2cd59
commit fbb5402bf2
5 changed files with 31 additions and 6 deletions

View File

@ -1,6 +1,6 @@
# jdbc.X
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate_dao_01?createDatabaseIfNotExist=true
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate3_01?createDatabaseIfNotExist=true
jdbc.user=tutorialuser
jdbc.pass=tutorialmy5ql

View File

@ -34,6 +34,7 @@ public class Child implements Serializable {
this.id = id;
}
@OneToOne(mappedBy = "child")
public Parent getParent() {
return parent;
}
@ -42,4 +43,13 @@ public class Child implements Serializable {
this.parent = parent;
}
//
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Child [id=").append(id).append(", parent=").append(parent).append("]");
return builder.toString();
}
}

View File

@ -4,9 +4,9 @@ import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
@ -16,7 +16,6 @@ public class Parent implements Serializable {
@GeneratedValue
private long id;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "parent", cascade = CascadeType.ALL)
private Child child;
public Parent() {
@ -39,6 +38,8 @@ public class Parent implements Serializable {
this.id = id;
}
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "child_fk")
public Child getChild() {
return child;
}
@ -47,4 +48,13 @@ public class Parent implements Serializable {
this.child = child;
}
//
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Parent [id=").append(id).append(", child=").append(child).append("]");
return builder.toString();
}
}

View File

@ -1,6 +1,6 @@
# jdbc.X
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate_dao_01?createDatabaseIfNotExist=true
jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate4_01?createDatabaseIfNotExist=true
jdbc.user=tutorialuser
jdbc.pass=tutorialmy5ql

View File

@ -32,9 +32,14 @@ public class ParentServicePersistenceIntegrationTest {
final Child childEntity = new Child();
childService.create(childEntity);
service.create(new Parent(childEntity));
final Parent parentEntity = new Parent(childEntity);
service.create(parentEntity);
System.out.println();
System.out.println("Child = " + childService.findOne(childEntity.getId()));
System.out.println("Child - parent = " + childService.findOne(childEntity.getId()).getParent());
System.out.println("Parent = " + service.findOne(parentEntity.getId()));
System.out.println("Parent - child = " + service.findOne(parentEntity.getId()).getChild());
}
}