Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...ore/src/test/java/org/hibernate/test/schemaupdate/inheritance/tableperclass/Category.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,19 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.test.schemaupdate.inheritance.tableperclass; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.Table; | ||
|
||
|
||
/** | ||
* @author Andrea Boriero | ||
*/ | ||
@Entity | ||
@Table(name = "CATEGORY") | ||
public class Category extends Element { | ||
} |
31 changes: 31 additions & 0 deletions
31
...core/src/test/java/org/hibernate/test/schemaupdate/inheritance/tableperclass/Element.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,31 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.test.schemaupdate.inheritance.tableperclass; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.Inheritance; | ||
import javax.persistence.InheritanceType; | ||
|
||
import org.hibernate.annotations.NaturalId; | ||
|
||
/** | ||
* @author Andrea Boriero | ||
*/ | ||
@Entity | ||
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) | ||
public abstract class Element { | ||
@Id | ||
@GeneratedValue | ||
private Long id; | ||
|
||
@Column(unique = true) | ||
@NaturalId | ||
private String code; | ||
} |
90 changes: 90 additions & 0 deletions
90
...st/java/org/hibernate/test/schemaupdate/inheritance/tableperclass/SchemaCreationTest.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,90 @@ | ||
/* | ||
* Hibernate, Relational Persistence for Idiomatic Java | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later. | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.test.schemaupdate.inheritance.tableperclass; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.Files; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
|
||
import org.hibernate.boot.MetadataSources; | ||
import org.hibernate.boot.registry.StandardServiceRegistry; | ||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | ||
import org.hibernate.boot.spi.MetadataImplementor; | ||
import org.hibernate.tool.hbm2ddl.SchemaExport; | ||
import org.hibernate.tool.schema.TargetType; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
|
||
import static org.hamcrest.core.Is.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
/** | ||
* @author Andrea Boriero | ||
*/ | ||
public class SchemaCreationTest { | ||
private File output; | ||
private StandardServiceRegistry ssr; | ||
private MetadataImplementor metadata; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
output = File.createTempFile( "update_script", ".sql" ); | ||
output.deleteOnExit(); | ||
ssr = new StandardServiceRegistryBuilder().build(); | ||
} | ||
|
||
@After | ||
public void tearsDown() { | ||
StandardServiceRegistryBuilder.destroy( ssr ); | ||
} | ||
|
||
@Test | ||
@TestForIssue(jiraKey = "HHH-10553") | ||
public void testUniqueConstraintIsCorrectlyGenerated() throws Exception { | ||
|
||
final MetadataSources metadataSources = new MetadataSources( ssr ); | ||
|
||
metadataSources.addAnnotatedClass( Element.class ); | ||
metadataSources.addAnnotatedClass( Category.class ); | ||
metadata = (MetadataImplementor) metadataSources.buildMetadata(); | ||
metadata.validate(); | ||
final SchemaExport schemaExport = new SchemaExport( ) | ||
.setHaltOnError( true ) | ||
.setOutputFile( output.getAbsolutePath() ) | ||
.setFormat( false ); | ||
schemaExport.create( EnumSet.of( TargetType.SCRIPT ), metadata ); | ||
|
||
final List<String> sqlLines = Files.readAllLines( output.toPath(), Charset.defaultCharset() ); | ||
|
||
boolean isUniqueConstraintCreated = false; | ||
for ( String statement : sqlLines ) { | ||
assertThat( | ||
"Should not try to create the unique constraint for the non existing table element", | ||
statement.toLowerCase().contains( "alter table element" ), | ||
is( false ) | ||
); | ||
if ( statement.toLowerCase().startsWith( "alter table category add constraint" ) | ||
&& statement.toLowerCase().contains( "unique (code)" ) ) { | ||
isUniqueConstraintCreated = true; | ||
|
||
} | ||
} | ||
|
||
assertThat( | ||
"Unique constraint for table category is not created", | ||
isUniqueConstraintCreated, | ||
is( true ) | ||
); | ||
} | ||
} |