Skip to content

mssql bug with locking #501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Entity1.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
49 changes: 49 additions & 0 deletions orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Entity2.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 39 additions & 0 deletions orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/Entity3.java
Original file line number Diff line number Diff line change
@@ -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 + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 = {
Expand All @@ -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());
} );
}
}
6 changes: 3 additions & 3 deletions orm/hibernate-orm-6/src/test/resources/hibernate.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
# See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
#

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
Expand Down