parent
24a317eec8
commit
9c51d269db
|
@ -0,0 +1,21 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||||
|
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" >
|
||||||
|
<hibernate-mapping package="org.hibernate.test.schemaupdate.manytomany" default-access="field">
|
||||||
|
<class entity-name="TestEntity" >
|
||||||
|
<id name="id" type="long">
|
||||||
|
<generator class="native"/>
|
||||||
|
</id>
|
||||||
|
|
||||||
|
<list name="children" table="test_entity_children" >
|
||||||
|
<key column="entity_id" foreign-key="fk_entity"/>
|
||||||
|
<index column="order"/>
|
||||||
|
<many-to-many column="child" entity-name="TestEntity" unique="true" foreign-key="fk_entity_children"/>
|
||||||
|
</list>
|
||||||
|
|
||||||
|
<set name="items" table="test_entity_item">
|
||||||
|
<key column="entity_id"/>
|
||||||
|
<element type="string" column="item" unique="true"/>
|
||||||
|
</set>
|
||||||
|
</class>
|
||||||
|
</hibernate-mapping>
|
|
@ -0,0 +1,21 @@
|
||||||
|
/*
|
||||||
|
* 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.uniqueconstraint;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Andrea Boriero
|
||||||
|
*/
|
||||||
|
public class TestEntity {
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private List<TestEntity> children;
|
||||||
|
|
||||||
|
private Set<String> items;
|
||||||
|
}
|
|
@ -0,0 +1,93 @@
|
||||||
|
/*
|
||||||
|
* 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.uniqueconstraint;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
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.cfg.Environment;
|
||||||
|
import org.hibernate.tool.hbm2ddl.SchemaExport;
|
||||||
|
import org.hibernate.tool.schema.TargetType;
|
||||||
|
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.core.Is.is;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Andrea Boriero
|
||||||
|
*/
|
||||||
|
public class UniqueConstraintGenerationTest {
|
||||||
|
private File output;
|
||||||
|
private MetadataImplementor metadata;
|
||||||
|
StandardServiceRegistry ssr;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() throws Exception {
|
||||||
|
output = File.createTempFile( "update_script", ".sql" );
|
||||||
|
output.deleteOnExit();
|
||||||
|
ssr = new StandardServiceRegistryBuilder()
|
||||||
|
.applySetting( Environment.HBM2DDL_AUTO, "none" )
|
||||||
|
.build();
|
||||||
|
metadata = (MetadataImplementor) new MetadataSources( ssr )
|
||||||
|
.addResource( "org/hibernate/test/schemaupdate/uniqueconstraint/TestEntity.hbm.xml" )
|
||||||
|
.buildMetadata();
|
||||||
|
metadata.validate();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
StandardServiceRegistryBuilder.destroy( ssr );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-11101")
|
||||||
|
public void testUniqueConstraintIsGenerated() throws Exception {
|
||||||
|
new SchemaExport()
|
||||||
|
.setOutputFile( output.getAbsolutePath() )
|
||||||
|
.create( EnumSet.of( TargetType.SCRIPT ), metadata );
|
||||||
|
|
||||||
|
assertThat(
|
||||||
|
"The test_entity_item table unique constraint has not been generated",
|
||||||
|
isUniqueConstraintGenerated( "test_entity_item", "item" ),
|
||||||
|
is( true )
|
||||||
|
);
|
||||||
|
|
||||||
|
assertThat(
|
||||||
|
"The test_entity_children table unique constraint has not been generated",
|
||||||
|
isUniqueConstraintGenerated( "test_entity_children", "child" ),
|
||||||
|
is( true )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isUniqueConstraintGenerated(String tableName, String columnName) throws IOException {
|
||||||
|
boolean matches = false;
|
||||||
|
final String regex = "alter table " + tableName + " add constraint uk_(.)* unique \\(" + columnName + "\\)";
|
||||||
|
|
||||||
|
final String fileContent = new String( Files.readAllBytes( output.toPath() ) ).toLowerCase();
|
||||||
|
final String[] split = fileContent.split( System.lineSeparator() );
|
||||||
|
Pattern p = Pattern.compile( regex );
|
||||||
|
for ( String line : split ) {
|
||||||
|
final Matcher matcher = p.matcher( line );
|
||||||
|
if ( matcher.matches() ) {
|
||||||
|
matches = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matches;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue