Last active
December 17, 2015 18:39
-
-
Save garbagetown/5654510 to your computer and use it in GitHub Desktop.
unexpected behavior about hibernate 3.6.10 with playframework 1.2.5.
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
Department(department_0): | |
code: "000" | |
name: "name_0" | |
Employee(empoyee_0): | |
code: "000" | |
name: "employee_0" | |
department: department_0 | |
Employee(empoyee_1): | |
code: "001" | |
name: "employee_1" | |
deleted_at: "2013-03-31" # this empoloyee is deleted logically. | |
department: department_0 | |
Employee(empoyee_2): | |
code: "002" | |
name: "employee_2" | |
department: department_0 |
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 models; | |
import java.util.Date; | |
import java.util.List; | |
import javax.persistence.Entity; | |
import javax.persistence.OneToMany; | |
import org.hibernate.Criteria; | |
import org.hibernate.FetchMode; | |
import org.hibernate.Session; | |
import org.hibernate.criterion.Restrictions; | |
import play.db.jpa.JPA; | |
import play.db.jpa.Model; | |
@Entity | |
public class Department extends Model { | |
public String code; | |
public String name; | |
public Date deleted_at; | |
@OneToMany(mappedBy = "department") | |
public List<Employee> employees; | |
public static Department findByCode(String code) { | |
Criteria c = ((Session) JPA.em().getDelegate()).createCriteria(Department.class); | |
c.setFetchMode("employees", FetchMode.JOIN); | |
c.createAlias("employees", "employee"); | |
c.add(Restrictions.eq("code", code)); | |
c.add(Restrictions.isNull("deleted_at")); | |
c.add(Restrictions.isNull("employee.deleted_at")); // fetch the employees that had been NOT deleted logically. | |
return (Department) c.uniqueResult(); | |
} | |
} |
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 models; | |
import org.junit.Before; | |
import org.junit.Test; | |
import play.test.Fixtures; | |
import play.test.UnitTest; | |
public class DepartmentTest extends UnitTest { | |
@Before | |
public void setUp() throws Exception { | |
Fixtures.deleteDatabase(); | |
Fixtures.loadModels("data.yml"); | |
} | |
@Test | |
public void testFindByCode() { | |
Department result = Department.findByCode("000"); | |
assertNotNull(result); | |
assertNotNull(result.employees); | |
assertEquals(2, result.employees.size()); // this assertion fails unexpectedly | |
} | |
} |
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 models; | |
import java.util.Date; | |
import javax.persistence.Entity; | |
import javax.persistence.JoinColumn; | |
import javax.persistence.ManyToOne; | |
import play.db.jpa.Model; | |
@Entity | |
public class Employee extends Model { | |
public String code; | |
public String name; | |
public Date deleted_at; | |
@JoinColumn(name = "department_id", referencedColumnName = "id") | |
@ManyToOne | |
public Department department; | |
} |
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
SELECT | |
employees0_.department_id AS department5_0_1_ | |
,employees0_.id AS id1_ | |
,employees0_.id AS id1_0_ | |
,employees0_.code AS code1_0_ | |
,employees0_.deleted_at AS deleted3_1_0_ | |
,employees0_.department_id AS department5_1_0_ | |
,employees0_.name AS name1_0_ | |
FROM | |
Employee employees0_ | |
WHERE | |
employees0_.department_id = ?; | |
-- why has this sql been generated??? It makes my test to fail. |
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
SELECT | |
this_.id AS id0_2_ | |
,this_.code AS code0_2_ | |
,this_.deleted_at AS deleted3_0_2_ | |
,this_.name AS name0_2_ | |
,employee1_.id AS id1_0_ | |
,employee1_.code AS code1_0_ | |
,employee1_.deleted_at AS deleted3_1_0_ | |
,employee1_.department_id AS department5_1_0_ | |
,employee1_.name AS name1_0_ | |
,department4_.id AS id0_1_ | |
,department4_.code AS code0_1_ | |
,department4_.deleted_at AS deleted3_0_1_ | |
,department4_.name AS name0_1_ | |
FROM | |
Department this_ INNER JOIN Employee employee1_ | |
ON this_.id = employee1_.department_id | |
LEFT OUTER JOIN Department department4_ -- why has this join statement been generated? | |
ON employee1_.department_id = department4_.id -- I think it's not necessary... | |
WHERE | |
this_.code = ? | |
AND this_.deleted_at IS null | |
AND employee1_.deleted_at IS null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you ever found out why this extra (unnecessary?) left outer join happens? :)