Skip to content

Commit

Permalink
OGM-931 Fixing typo, using manually assigned ids for the sake of unde…
Browse files Browse the repository at this point in the history
…rstandability
  • Loading branch information
gunnarmorling committed Dec 15, 2015
1 parent 9a05ba0 commit e09f640
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
Expand Up @@ -9,15 +9,13 @@
import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "Employee")
Expand All @@ -26,11 +24,9 @@ public class Employee implements Serializable {
private static final long serialVersionUID = -8732345803771451030L;
private String id;
private String name;
private Employeer employeer;
private Employer employer;

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
public String getId() {
return id;
}
Expand All @@ -50,12 +46,12 @@ public void setName(String name) {
@ManyToOne
@Cascade(value = { CascadeType.MERGE })
@JoinColumn(insertable = true, updatable = true, name = "EmployerID")
public Employeer getEmployeer() {
return employeer;
public Employer getEmployer() {
return employer;
}

public void setEmployeer(Employeer employeer) {
this.employeer = employeer;
public void setEmployer(Employer employer) {
this.employer = employer;
}

}
Expand Up @@ -12,18 +12,14 @@

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "Employer")
public class Employeer implements Serializable {
public class Employer implements Serializable {

private static final long serialVersionUID = -8732345803771451030L;
private String id;
Expand All @@ -32,8 +28,6 @@ public class Employeer implements Serializable {
private Set<Employee> employees = new HashSet<>();

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
public String getId() {
return id;
}
Expand All @@ -50,7 +44,7 @@ public void setName(String name) {
this.name = name;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "employeer")
@OneToMany(fetch = FetchType.EAGER, mappedBy = "employer")
@Cascade({ CascadeType.PERSIST, CascadeType.SAVE_UPDATE, CascadeType.DELETE })
public Set<Employee> getEmployees() {
return employees;
Expand Down
Expand Up @@ -56,10 +56,10 @@ public void testUnidirectionalManyToOne() throws Exception {
session.clear();

transaction = session.beginTransaction();
emmanuel = (Member) session.get( Member.class, emmanuel.getId() );
emmanuel = session.get( Member.class, emmanuel.getId() );
jug = emmanuel.getMemberOf();
session.delete( emmanuel );
jerome = (Member) session.get( Member.class, jerome.getId() );
jerome = session.get( Member.class, jerome.getId() );
session.delete( jerome );
session.delete( jug );
transaction.commit();
Expand All @@ -76,20 +76,22 @@ public void testAssociationOwnerNotManagedException() throws Exception {
thrown.expect( HibernateException.class );
thrown.expectMessage( "OGM000082" );

Employeer employeer = new Employeer();
employeer.setName( "Hibernate" );
Employer employer = new Employer();
employer.setId( "employer-1" );
employer.setName( "Hibernate" );

Session session = openSession();
Transaction transaction = session.beginTransaction();
session.save( employeer );
session.save( employer );
session.flush();
transaction.commit();
session.clear();

// Create Employee and Map it with Employeer.
Employee employee = new Employee();
employee.setId( "employee-1" );
employee.setName( "DNadar" );
employee.setEmployeer( employeer );
employee.setEmployer( employer );

try {
transaction = session.beginTransaction();
Expand All @@ -106,7 +108,7 @@ public void testAssociationOwnerNotManagedException() throws Exception {

session = openSession();
transaction = session.beginTransaction();
session.delete( session.get( Employeer.class, employeer.getId() ) );
session.delete( session.get( Employer.class, employer.getId() ) );
Employee saved = session.get( Employee.class, employee.getId() );
if ( saved != null ) {
session.delete( saved );
Expand Down Expand Up @@ -145,18 +147,18 @@ public void testBidirectionalManyToOneRegular() throws Exception {
session.clear();

transaction = session.beginTransaction();
force = (SalesForce) session.get( SalesForce.class, force.getId() );
force = session.get( SalesForce.class, force.getId() );
assertNotNull( force.getSalesGuys() );
assertEquals( 2, force.getSalesGuys().size() );
simon = (SalesGuy) session.get( SalesGuy.class, simon.getId() );
simon = session.get( SalesGuy.class, simon.getId() );
// purposely faulty
// force.getSalesGuys().remove( simon );
session.delete( simon );
transaction.commit();
session.clear();

transaction = session.beginTransaction();
force = (SalesForce) session.get( SalesForce.class, force.getId() );
force = session.get( SalesForce.class, force.getId() );
assertNotNull( force.getSalesGuys() );
assertEquals( 1, force.getSalesGuys().size() );
session.delete( force.getSalesGuys().iterator().next() );
Expand Down Expand Up @@ -190,16 +192,16 @@ public void testBidirectionalManyToOneRemoval() throws Exception {

// removing one sales guy, leaving the other in place
transaction = session.beginTransaction();
force = (SalesForce) session.get( SalesForce.class, force.getId() );
force = session.get( SalesForce.class, force.getId() );
assertEquals( 2, force.getSalesGuys().size() );
SalesGuy salesGuy = (SalesGuy) session.get( SalesGuy.class, eric.getId() );
SalesGuy salesGuy = session.get( SalesGuy.class, eric.getId() );
salesGuy.setSalesForce( null );
force.getSalesGuys().remove( salesGuy );
transaction.commit();
session.clear();

transaction = session.beginTransaction();
force = (SalesForce) session.get( SalesForce.class, force.getId() );
force = session.get( SalesForce.class, force.getId() );
assertEquals( 1, force.getSalesGuys().size() );
salesGuy = force.getSalesGuys().iterator().next();
assertThat( salesGuy.getName() ).isEqualTo( "Simon" );
Expand Down Expand Up @@ -293,11 +295,11 @@ public void testRemovalOfTransientEntityWithAssociation() throws Exception {

transaction = session.beginTransaction();

SalesGuy salesGuy = (SalesGuy) session.get( SalesGuy.class, "eric" );
SalesGuy salesGuy = session.get( SalesGuy.class, "eric" );
assertThat( salesGuy.getSalesForce() ).describedAs( "Stale association should be exposed as null" ).isNull();
session.delete( salesGuy );

salesGuy = (SalesGuy) session.get( SalesGuy.class, "simon" );
salesGuy = session.get( SalesGuy.class, "simon" );
assertThat( salesGuy.getSalesForce() ).describedAs( "Stale association should be exposed as null" ).isNull();
session.delete( salesGuy );

Expand Down Expand Up @@ -337,7 +339,7 @@ public void testDefaultBiDirManyToOneCompositeKeyTest() throws Exception {
session.clear();

transaction = session.beginTransaction();
Court localCourt = (Court) session.get( Court.class, new Court.CourtId( "DE", 123 ) );
Court localCourt = session.get( Court.class, new Court.CourtId( "DE", 123 ) );
assertThat( localCourt.getGames() ).hasSize( 2 );
for ( Game game : localCourt.getGames() ) {
session.delete( game );
Expand All @@ -361,7 +363,7 @@ protected Class<?>[] getAnnotatedClasses() {
Game.class,
Court.class,
Employee.class,
Employeer.class
Employer.class
};
}
}

0 comments on commit e09f640

Please sign in to comment.