Skip to content

Commit c7abe77

Browse files
committed
HHH-10304 - ImplicitCompositeKeyJoinTest#testImplicitCompositeJoin fails due to dialect-specific differences in generated SQL string
1 parent 977c3f9 commit c7abe77

File tree

1 file changed

+57
-97
lines changed

1 file changed

+57
-97
lines changed

hibernate-core/src/test/java/org/hibernate/test/schemaupdate/ImplicitCompositeKeyJoinTest.java

Lines changed: 57 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.hibernate.testing.TestForIssue;
2929

3030
import static org.junit.Assert.assertTrue;
31+
import static org.junit.Assert.fail;
3132

3233
/**
3334
* @author Andrea Boriero
@@ -36,47 +37,85 @@
3637
public class ImplicitCompositeKeyJoinTest {
3738
private static final Logger LOGGER = Logger.getLogger( ImplicitCompositeKeyJoinTest.class );
3839

39-
private final static String EXPECTED_SQL = "create table Employee " +
40-
"(age varchar(15) not null" +
41-
", birthday varchar(255) not null" +
42-
", name varchar(20) not null" +
43-
", manager_age varchar(15)" +
44-
", manager_birthday varchar(255)" +
45-
", manager_name varchar(20)" +
46-
", primary key (age, birthday, name))";
47-
4840
@Test
49-
public void testImplicitCompositeJoin() throws Exception {
50-
51-
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
41+
public void testSchemaCreationSQLCommandIsGeneratedWithTheCorrectColumnSizeValues() throws Exception {
42+
final StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
5243
try {
53-
org.hibernate.boot.Metadata metadata = new MetadataSources( ssr )
44+
final org.hibernate.boot.Metadata metadata = new MetadataSources( ssr )
5445
.addAnnotatedClass( Employee.class )
5546
.buildMetadata();
5647

57-
boolean passed = false;
48+
boolean createTableEmployeeFound = false;
5849

59-
List<String> commands = new SchemaCreatorImpl().generateCreationCommands(
50+
final List<String> commands = new SchemaCreatorImpl().generateCreationCommands(
6051
metadata,
6152
false
6253
);
54+
6355
for ( String command : commands ) {
6456
LOGGER.info( command );
57+
if ( command.toLowerCase().contains( "create table employee" ) ) {
58+
final String[] columnsDefinition = getColumnsDefinition( command );
6559

66-
if ( EXPECTED_SQL.equals( command ) ) {
67-
passed = true;
60+
for ( int i = 0; i < columnsDefinition.length; i++ ) {
61+
checkColumnSize( columnsDefinition[i] );
62+
}
63+
createTableEmployeeFound = true;
6864
}
6965
}
7066
assertTrue(
7167
"Expected create table command for Employee entity not found",
72-
passed
68+
createTableEmployeeFound
7369
);
7470
}
7571
finally {
7672
StandardServiceRegistryBuilder.destroy( ssr );
7773
}
7874
}
7975

76+
private String[] getColumnsDefinition(String command) {
77+
String substring = command.toLowerCase().replaceAll( "create table employee ", "" );
78+
substring = substring.substring( 0, substring.toLowerCase().indexOf( "primary key" ) );
79+
return substring.split( "\\," );
80+
}
81+
82+
private void checkColumnSize(String s) {
83+
if ( s.toLowerCase().contains( "manager_age" ) ) {
84+
if ( !s.contains( "15" ) ) {
85+
fail( expectedMessage( "manager_age", 15, s ) );
86+
}
87+
}
88+
else if ( s.toLowerCase().contains( "manager_birthday" ) ) {
89+
if ( !s.contains( "255" ) ) {
90+
fail( expectedMessage( "manager_birthday", 255, s ) );
91+
}
92+
}
93+
else if ( s.toLowerCase().contains( "manager_name" ) ) {
94+
if ( !s.contains( "20" ) ) {
95+
fail( expectedMessage( "manager_name", 20, s ) );
96+
}
97+
}
98+
else if ( s.toLowerCase().contains( "age" ) ) {
99+
if ( !s.contains( "15" ) ) {
100+
fail( expectedMessage( "age", 15, s ) );
101+
}
102+
}
103+
else if ( s.toLowerCase().contains( "birthday" ) ) {
104+
if ( !s.contains( "255" ) ) {
105+
fail( expectedMessage( "birthday", 255, s ) );
106+
}
107+
}
108+
else if ( s.toLowerCase().contains( "name" ) ) {
109+
if ( !s.contains( "20" ) ) {
110+
fail( expectedMessage( "name", 20, s ) );
111+
}
112+
}
113+
}
114+
115+
private String expectedMessage(String column_name, int size, String actual) {
116+
return "Expected " + column_name + " " + size + " but was " + actual;
117+
}
118+
80119
@Entity
81120
@Table(name = "Employee")
82121
public class Employee {
@@ -87,95 +126,16 @@ public class Employee {
87126
@ManyToOne(optional = true)
88127
@ForeignKey(name = "none")
89128
private Employee manager;
90-
91-
public void setId(EmployeeId id) {
92-
this.id = id;
93-
}
94-
95-
public EmployeeId getId() {
96-
return id;
97-
}
98-
99-
public void setManager(Employee manager) {
100-
this.manager = manager;
101-
}
102-
103-
public Employee getManager() {
104-
return manager;
105-
}
106129
}
107130

108131
@Embeddable
109132
public class EmployeeId implements Serializable {
110-
private static final long serialVersionUID = 1L;
111-
112-
public EmployeeId(String name, String birthday, String age) {
113-
this.name = name;
114-
this.birthday = birthday;
115-
this.age = age;
116-
}
117-
118133
@Column(length = 15)
119134
public String age;
120135

121136
@Column(length = 20)
122137
private String name;
123138

124139
private String birthday;
125-
126-
@Override
127-
public int hashCode() {
128-
int hash = 1;
129-
hash = hash * 31 + (name != null ? name.hashCode() : 0);
130-
hash = hash * 31 + (age != null ? age.hashCode() : 0);
131-
return hash * 31 + (birthday != null ? birthday.hashCode() : 0);
132-
}
133-
134-
@Override
135-
public boolean equals(Object obj) {
136-
if ( obj == this ) {
137-
return true;
138-
}
139-
140-
if ( !(obj instanceof EmployeeId) ) {
141-
return false;
142-
}
143-
EmployeeId that = (EmployeeId) obj;
144-
if ( age != that.age ) {
145-
return false;
146-
}
147-
if ( birthday != that.birthday ) {
148-
return false;
149-
}
150-
if ( name != null && !name.equals( that.name ) ) {
151-
return false;
152-
}
153-
return true;
154-
}
155-
156-
public void setAge(String age) {
157-
this.age = age;
158-
}
159-
160-
public void setName(String name) {
161-
this.name = name;
162-
}
163-
164-
165-
public void setBirthday(String birthday) {
166-
this.birthday = birthday;
167-
}
168-
169-
public String getAge() {
170-
return age;
171-
}
172-
173-
public String getName() {
174-
return name;
175-
}
176-
177-
public String getBirthday() {
178-
return birthday;
179-
}
180140
}
181141
}

0 commit comments

Comments
 (0)