diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Entity1.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Entity1.java new file mode 100644 index 00000000..add10cb7 --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Entity1.java @@ -0,0 +1,57 @@ +package org.hibernate.bugs; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToOne; + +@Entity +public class Entity1 { + + @Column + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column + private String name; + + @OneToOne + private Entity3 entity3; + + public Entity1(Long id, Entity3 entity3) { + this.id = id; + this.entity3 = entity3; + } + + public Entity1() { + + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Entity3 getEntity3() { + return entity3; + } + + public void setEntity3(Entity3 entity3) { + this.entity3 = entity3; + } + + @Override + public String toString() { + return "Entity1{" + + "id=" + id + + ", name='" + name + '\'' + + ", entity3=" + entity3 + + '}'; + } +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Entity2.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Entity2.java new file mode 100644 index 00000000..4ce6a69e --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Entity2.java @@ -0,0 +1,49 @@ +package org.hibernate.bugs; + +import jakarta.persistence.Column; +import jakarta.persistence.DiscriminatorColumn; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Inheritance; +import jakarta.persistence.InheritanceType; + +@Entity +@Inheritance(strategy = InheritanceType.SINGLE_TABLE) +@DiscriminatorColumn +public class Entity2 { + + @Column + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column + private String name; + + public Entity2() { + + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Entity2(Long id, String name) { + this.id = id; + this.name = name; + } +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Entity3.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Entity3.java new file mode 100644 index 00000000..99aabd6a --- /dev/null +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Entity3.java @@ -0,0 +1,39 @@ +package org.hibernate.bugs; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; + +@Entity +public class Entity3 extends Entity2 { + + @Column + private String surname; + + public Entity3() { + + } + + public String getSurname() { + return surname; + } + + public void setSurname(String surname) { + this.surname = surname; + } + + public Entity3(String surname) { + this.surname = surname; + } + + public Entity3(Long id, String name, String surname) { + super(id, name); + this.surname = surname; + } + + @Override + public String toString() { + return "Entity3{" + + "surname='" + surname + '\'' + + '}'; + } +} diff --git a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java index eefb7df4..6bcd592f 100644 --- a/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java +++ b/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/ORMUnitTestCase.java @@ -15,8 +15,8 @@ */ package org.hibernate.bugs; +import jakarta.persistence.LockModeType; import org.hibernate.cfg.AvailableSettings; - import org.hibernate.testing.orm.junit.DomainModel; import org.hibernate.testing.orm.junit.ServiceRegistry; import org.hibernate.testing.orm.junit.SessionFactory; @@ -35,9 +35,9 @@ */ @DomainModel( annotatedClasses = { - // Add your entities here. - // Foo.class, - // Bar.class + Entity1.class, + Entity2.class, + Entity3.class }, // If you use *.hbm.xml mappings, instead of annotations, add the mappings here. xmlMappings = { @@ -64,7 +64,39 @@ class ORMUnitTestCase { @Test void hhh123Test(SessionFactoryScope scope) throws Exception { scope.inTransaction( session -> { - // Do stuff... + Entity3 entity3 = new Entity3(); + entity3.setName("test"); + entity3.setSurname("tester"); + session.persist(entity3); + Entity1 entity1 = new Entity1(); + entity1.setEntity3(entity3); + session.persist(entity1); + session.flush(); + session.clear(); + Long id = entity1.getId(); // or Long, depending on your ID type + Entity1 entity1fromDb = session.find(Entity1.class, id, LockModeType.PESSIMISTIC_WRITE); + /* + Hibernate: + select + e1_0.id, + e2_0.id, + e2_0.name, + e2_0.surname, + e1_0.name + from + Entity1 e1_0 with (updlock, holdlock, rowlock) -- this is fine it is not subselect + left join + (select + * + from + Entity2 t + where + t.DTYPE='Entity3') e2_0 with (updlock, holdlock, rowlock) -- here is the problem that fails on real MSSQL database + on e2_0.id=e1_0.entity3_id + where + e1_0.id=? + */ + System.out.println("Gotcha!" + entity1fromDb.toString()); } ); } } diff --git a/orm/hibernate-orm-6/src/test/resources/hibernate.properties b/orm/hibernate-orm-6/src/test/resources/hibernate.properties index 9c894d7b..ff97f52c 100644 --- a/orm/hibernate-orm-6/src/test/resources/hibernate.properties +++ b/orm/hibernate-orm-6/src/test/resources/hibernate.properties @@ -5,16 +5,16 @@ # See the lgpl.txt file in the root directory or . # -hibernate.dialect org.hibernate.dialect.H2Dialect +hibernate.dialect org.hibernate.dialect.SQLServerDialect hibernate.connection.driver_class org.h2.Driver #hibernate.connection.url jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE -hibernate.connection.url jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1 +hibernate.connection.url jdbc:h2:mem:db1;MODE=MSSQLServer;DB_CLOSE_DELAY=-1 hibernate.connection.username sa hibernate.connection.password hibernate.connection.pool_size 5 -hibernate.show_sql false +hibernate.show_sql true hibernate.format_sql true hibernate.max_fetch_depth 5