Skip to content
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

Add test case demonstrate @TenantId not works properly #411

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
2 changes: 1 addition & 1 deletion orm/hibernate-orm-6/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<properties>
<version.com.h2database>2.3.230</version.com.h2database>
<version.junit>4.13.2</version.junit>
<version.org.hibernate.orm>6.5.2.Final</version.org.hibernate.orm>
<version.org.hibernate.orm>6.6.0.CR1</version.org.hibernate.orm>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
*/
package org.hibernate.bugs;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.annotations.TenantId;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

Expand All @@ -35,10 +40,9 @@ public class ORMUnitTestCase extends BaseCoreFunctionalTestCase {

// Add your entities here.
@Override
protected Class[] getAnnotatedClasses() {
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
// Foo.class,
// Bar.class
TestEntity.class
};
}

Expand All @@ -63,17 +67,72 @@ protected void configure(Configuration configuration) {

configuration.setProperty( AvailableSettings.SHOW_SQL, Boolean.TRUE.toString() );
configuration.setProperty( AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString() );
configuration.setProperty( AvailableSettings.MULTI_TENANT_IDENTIFIER_RESOLVER, TenantIdentifierResolver.class.getName() );
//configuration.setProperty( AvailableSettings.GENERATE_STATISTICS, "true" );
}

// Add your tests, using standard JUnit.
@Test
public void hhh123Test() throws Exception {
// BaseCoreFunctionalTestCase automatically creates the SessionFactory and provides the Session.
public void testMergeWithTenantId() {
Session s = openSession();
Transaction tx = s.beginTransaction();
// Do stuff...
TestEntity entity = new TestEntity();
entity.name = "test";
s.persist(entity);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
entity = s.find(TestEntity.class, entity.id);
entity.name = "test2";
tx.commit(); // generated update SQL doesn't contains `where tenant = ?`
s.close();
}

@Test
public void testRemoveWithTenantId() {
Session s = openSession();
Transaction tx = s.beginTransaction();
TestEntity entity = new TestEntity();
entity.name = "test";
s.persist(entity);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
entity = s.find(TestEntity.class, entity.id);
s.remove(entity);
tx.commit(); // generated delete SQL doesn't contains `where tenant = ?`
s.close();
}

@Entity(name = "TestEntity")
static class TestEntity {
@Id
@GeneratedValue
Long id;

String name;

@TenantId
String tenant;
}

public static class TenantIdentifierResolver implements CurrentTenantIdentifierResolver<String> {

@Override
public String resolveCurrentTenantIdentifier() {
return "test";
}

@Override
public boolean validateExistingCurrentSessions() {
return false;
}

@Override
public boolean isRoot(String tenantId) {
return "root".equals(tenantId);
}
}
}