This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for different ID column names in entities
- Loading branch information
Showing
7 changed files
with
220 additions
and
190 deletions.
There are no files selected for viewing
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
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
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
96 changes: 96 additions & 0 deletions
96
...est/java/lt/grimsa/hibernate/id/AbstractSingleGlobalTemporaryTableBulkIdStrategyTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package lt.grimsa.hibernate.id; | ||
|
||
import model.TestEntities.*; | ||
import org.apache.log4j.AppenderSkeleton; | ||
import org.apache.log4j.Level; | ||
import org.apache.log4j.Logger; | ||
import org.apache.log4j.spi.LoggingEvent; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.cfg.AvailableSettings; | ||
import org.hibernate.cfg.Configuration; | ||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
public abstract class AbstractSingleGlobalTemporaryTableBulkIdStrategyTest extends BaseCoreFunctionalTestCase { | ||
private Logger sqlLogger = Logger.getLogger("org.hibernate.SQL"); | ||
private MessageCapturingAppender sqlAppender; | ||
private boolean ddlExecuted; | ||
|
||
@Override | ||
protected void configure(Configuration configuration) { | ||
configuration.setProperty(AvailableSettings.HQL_BULK_ID_STRATEGY, SingleGlobalTemporaryTableBulkIdStrategy.class.getName()); | ||
configuration.setProperty(SingleGlobalTemporaryTableBulkIdStrategy.TABLE, "HT_TEMP_IDS"); | ||
configuration.setProperty(SingleGlobalTemporaryTableBulkIdStrategy.DISCRIMINATOR_COLUMN, "ENTITY_NAME"); | ||
} | ||
|
||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class<?>[]{Animal.class, Mammal.class, Reptile.class, Human.class, Dog.class}; | ||
} | ||
|
||
@Override | ||
protected void prepareTest() throws Exception { | ||
sqlAppender = new MessageCapturingAppender(); | ||
openSession(); | ||
if (!ddlExecuted) { | ||
doInTransaction(() -> session.createNativeQuery("create global temporary table HT_TEMP_IDS (" | ||
+ getIdColumnName() + " CHAR(36), " | ||
+ getEntityColumnName() + " VARCHAR(100))") | ||
.executeUpdate()); | ||
ddlExecuted = true; | ||
} | ||
sqlLogger.addAppender(sqlAppender); | ||
} | ||
|
||
protected String getIdColumnName() { | ||
return "ID"; | ||
} | ||
|
||
protected String getEntityColumnName() { | ||
return "ENTITY_NAME"; | ||
} | ||
|
||
@Override | ||
protected void cleanupTest() throws Exception { | ||
sqlLogger.removeAppender(sqlAppender); | ||
} | ||
|
||
protected void doInTransaction(Runnable runnable) { | ||
Transaction transaction = session.beginTransaction(); | ||
runnable.run(); | ||
transaction.commit(); | ||
} | ||
|
||
protected void doWithLogging(Runnable runnable) { | ||
sqlLogger.setLevel(Level.DEBUG); | ||
runnable.run(); | ||
sqlLogger.setLevel(Level.OFF); | ||
} | ||
|
||
protected void verify(Predicate<List<String>> sqlLogConsumingPredicate) { | ||
assertTrue(sqlLogConsumingPredicate.test(sqlAppender.log)); | ||
} | ||
|
||
private static class MessageCapturingAppender extends AppenderSkeleton { | ||
private final List<String> log = new ArrayList<>(); | ||
|
||
@Override | ||
protected void append(LoggingEvent event) { | ||
log.add(event.getRenderedMessage()); | ||
} | ||
|
||
@Override | ||
public boolean requiresLayout() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
} | ||
} | ||
} |
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
Oops, something went wrong.