From 2ac8c0c0072d6182af343c83928db4b8b2053427 Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Wed, 12 Oct 2011 19:43:32 +0200 Subject: [PATCH 1/2] HHH-6068 - Honor JPA default schema and catalog --- .../main/java/org/hibernate/cfg/Configuration.java | 13 ++++++++++++- .../annotations/reflection/JPAMetadataProvider.java | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java index a825f64536..64a93d7f2f 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java @@ -1306,10 +1306,21 @@ public class Configuration implements Serializable { { if ( !isDefaultProcessed ) { //use global delimiters if orm.xml declare it - final Object isDelimited = reflectionManager.getDefaults().get( "delimited-identifier" ); + Map defaults = reflectionManager.getDefaults(); + final Object isDelimited = defaults.get( "delimited-identifier" ); if ( isDelimited != null && isDelimited == Boolean.TRUE ) { getProperties().put( Environment.GLOBALLY_QUOTED_IDENTIFIERS, "true" ); } + // Set default schema name if orm.xml declares it. + final String schema = (String) defaults.get( "schema" ); + if ( StringHelper.isNotEmpty( schema ) ) { + getProperties().put( Environment.DEFAULT_SCHEMA, schema ); + } + // Set default catalog name if orm.xml declares it. + final String catalog = (String) defaults.get( "catalog" ); + if ( StringHelper.isNotEmpty( catalog ) ) { + getProperties().put( Environment.DEFAULT_CATALOG, catalog ); + } AnnotationBinder.bindDefaults( createMappings() ); isDefaultProcessed = true; diff --git a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/JPAMetadataProvider.java b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/JPAMetadataProvider.java index 8f414beda9..da8a95d69d 100644 --- a/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/JPAMetadataProvider.java +++ b/hibernate-core/src/main/java/org/hibernate/cfg/annotations/reflection/JPAMetadataProvider.java @@ -80,6 +80,8 @@ public class JPAMetadataProvider implements MetadataProvider, Serializable { defaults = new HashMap(); XMLContext.Default xmlDefaults = xmlContext.getDefault( null ); + defaults.put( "schema", xmlDefaults.getSchema() ); + defaults.put( "catalog", xmlDefaults.getCatalog() ); defaults.put( "delimited-identifier", xmlDefaults.getDelimitedIdentifier() ); List entityListeners = new ArrayList(); for ( String className : xmlContext.getDefaultEntityListeners() ) { From 0645acf04f023fd7771637497680399db9302c7a Mon Sep 17 00:00:00 2001 From: Lukasz Antoniak Date: Sat, 15 Oct 2011 00:29:42 +0200 Subject: [PATCH 2/2] HHH-6068 - Test case --- .../id/sequences/HibernateSequenceTest.java | 64 +++++++++++++++++++ .../entities/HibernateSequenceEntity.java | 34 ++++++++++ .../test/annotations/id/sequences/orm.xml | 12 ++++ 3 files changed, 110 insertions(+) create mode 100644 hibernate-core/src/matrix/java/org/hibernate/test/annotations/id/sequences/HibernateSequenceTest.java create mode 100644 hibernate-core/src/matrix/java/org/hibernate/test/annotations/id/sequences/entities/HibernateSequenceEntity.java create mode 100644 hibernate-core/src/matrix/resources/org/hibernate/test/annotations/id/sequences/orm.xml diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/id/sequences/HibernateSequenceTest.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/id/sequences/HibernateSequenceTest.java new file mode 100644 index 0000000000..014f9b9b2f --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/id/sequences/HibernateSequenceTest.java @@ -0,0 +1,64 @@ +package org.hibernate.test.annotations.id.sequences; + +import org.junit.Assert; +import org.junit.Test; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; +import org.hibernate.id.IdentifierGenerator; +import org.hibernate.id.enhanced.SequenceStyleGenerator; +import org.hibernate.mapping.Table; +import org.hibernate.persister.entity.EntityPersister; +import org.hibernate.test.annotations.id.sequences.entities.HibernateSequenceEntity; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@TestForIssue(jiraKey = "HHH-6068") +public class HibernateSequenceTest extends BaseCoreFunctionalTestCase { + private static final String SCHEMA_NAME = "OTHER_SCHEMA"; + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + HibernateSequenceEntity.class + }; + } + + @Override + protected void configure(Configuration cfg) { + super.configure( cfg ); + cfg.addResource( "org/hibernate/test/annotations/id/sequences/orm.xml" ); + cfg.setProperty( + Environment.URL, cfg.getProperty( Environment.URL ) + ";INIT=CREATE SCHEMA IF NOT EXISTS " + SCHEMA_NAME + ); + } + + @Test + public void testHibernateSequenceSchema() { + EntityPersister persister = sessionFactory().getEntityPersister( HibernateSequenceEntity.class.getName() ); + IdentifierGenerator generator = persister.getIdentifierGenerator(); + Assert.assertTrue( SequenceStyleGenerator.class.isInstance( generator ) ); + SequenceStyleGenerator seqGenerator = (SequenceStyleGenerator) generator; + Assert.assertEquals( + Table.qualify( null, SCHEMA_NAME, SequenceStyleGenerator.DEF_SEQUENCE_NAME ), + seqGenerator.getDatabaseStructure().getName() + ); + } + + @Test + public void testHibernateSequenceNextVal() { + Session session = openSession(); + Transaction txn = session.beginTransaction(); + HibernateSequenceEntity entity = new HibernateSequenceEntity(); + entity.setText( "sample text" ); + session.save( entity ); + txn.commit(); + session.close(); + Assert.assertNotNull( entity.getId() ); + } +} diff --git a/hibernate-core/src/matrix/java/org/hibernate/test/annotations/id/sequences/entities/HibernateSequenceEntity.java b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/id/sequences/entities/HibernateSequenceEntity.java new file mode 100644 index 0000000000..6d2b840594 --- /dev/null +++ b/hibernate-core/src/matrix/java/org/hibernate/test/annotations/id/sequences/entities/HibernateSequenceEntity.java @@ -0,0 +1,34 @@ +package org.hibernate.test.annotations.id.sequences.entities; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@Entity +public class HibernateSequenceEntity { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private Long id; + + private String text; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } +} diff --git a/hibernate-core/src/matrix/resources/org/hibernate/test/annotations/id/sequences/orm.xml b/hibernate-core/src/matrix/resources/org/hibernate/test/annotations/id/sequences/orm.xml new file mode 100644 index 0000000000..86d9dfcb9b --- /dev/null +++ b/hibernate-core/src/matrix/resources/org/hibernate/test/annotations/id/sequences/orm.xml @@ -0,0 +1,12 @@ + + + + + OTHER_SCHEMA + + + \ No newline at end of file