Skip to content

Commit 825468c

Browse files
committed
HHH-9908 - Regression in naming collection join tables
(cherry picked from commit 51a8bc7)
1 parent ed867b2 commit 825468c

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.test.namingstrategy.collectionJoinTableNaming;
25+
26+
import java.io.Serializable;
27+
import java.util.List;
28+
import javax.persistence.CascadeType;
29+
import javax.persistence.Entity;
30+
import javax.persistence.FetchType;
31+
import javax.persistence.GeneratedValue;
32+
import javax.persistence.GenerationType;
33+
import javax.persistence.Id;
34+
import javax.persistence.ManyToMany;
35+
import javax.persistence.OrderColumn;
36+
import javax.persistence.Table;
37+
38+
import org.hibernate.annotations.GenericGenerator;
39+
import org.hibernate.cfg.Configuration;
40+
import org.hibernate.cfg.EJB3NamingStrategy;
41+
import org.hibernate.cfg.ImprovedNamingStrategy;
42+
import org.hibernate.mapping.Collection;
43+
import org.hibernate.mapping.Column;
44+
import org.hibernate.tool.hbm2ddl.SchemaExport;
45+
46+
import org.hibernate.testing.TestForIssue;
47+
import org.hibernate.testing.junit4.BaseUnitTestCase;
48+
import org.junit.Test;
49+
50+
import static org.junit.Assert.assertEquals;
51+
import static org.junit.Assert.assertFalse;
52+
import static org.junit.Assert.assertSame;
53+
import static org.junit.Assert.assertTrue;
54+
55+
/**
56+
* @author Steve Ebersole
57+
* @author Alessandro Polverini
58+
*/
59+
public class CollectionJoinTableNamingTest extends BaseUnitTestCase {
60+
@Test
61+
@TestForIssue( jiraKey = "HHH-9908" )
62+
public void testCollectionJoinTableNamingLegacyStrategy() {
63+
Configuration cfg = new Configuration();
64+
cfg.setNamingStrategy( ImprovedNamingStrategy.INSTANCE );
65+
66+
cfg.addAnnotatedClass( Input.class );
67+
cfg.addAnnotatedClass( Ptx.class );
68+
cfg.buildMappings();
69+
70+
Collection inputs1Mapping = cfg.getCollectionMapping( Ptx.class.getName() + ".inputs1" );
71+
assertEquals( "ptx_inputs1", inputs1Mapping.getCollectionTable().getName() );
72+
73+
Collection inputs2Mapping = cfg.getCollectionMapping( Ptx.class.getName() + ".inputs2" );
74+
assertEquals( "ptx_inputs2", inputs2Mapping.getCollectionTable().getName() );
75+
}
76+
77+
@Test
78+
@TestForIssue( jiraKey = "HHH-9908" )
79+
public void testCollectionJoinTableNamingJpaCompliantStrategy() {
80+
// Even in 4.3, with JPA compliant naming, Hibernate creates an unusable table...
81+
82+
Configuration cfg = new Configuration();
83+
cfg.setNamingStrategy( EJB3NamingStrategy.INSTANCE );
84+
85+
cfg.addAnnotatedClass( Input.class );
86+
cfg.addAnnotatedClass( Ptx.class );
87+
cfg.buildMappings();
88+
89+
Collection inputs1Mapping = cfg.getCollectionMapping( Ptx.class.getName() + ".inputs1" );
90+
assertEquals( "ptx_input", inputs1Mapping.getCollectionTable().getName() );
91+
92+
Collection inputs2Mapping = cfg.getCollectionMapping( Ptx.class.getName() + ".inputs2" );
93+
assertEquals( "ptx_input", inputs2Mapping.getCollectionTable().getName() );
94+
95+
assertSame( inputs1Mapping.getCollectionTable(), inputs2Mapping.getCollectionTable() );
96+
97+
new SchemaExport( cfg ).create( true, false );
98+
99+
for ( int i = 0; i < inputs1Mapping.getCollectionTable().getColumnSpan(); i++ ) {
100+
final Column column = inputs1Mapping.getCollectionTable().getColumn( i );
101+
102+
// this, coupled with JPA saying the 2 collections implicitly map to the same table,
103+
// is the crux of the problem: all columns are null, so we effectively can never
104+
// insert rows into it.
105+
assertFalse( column.isNullable() );
106+
}
107+
}
108+
109+
@Entity
110+
@Table(name = "ptx")
111+
public static class Ptx {
112+
@Id
113+
@GeneratedValue(strategy = GenerationType.AUTO, generator = "increment")
114+
@GenericGenerator(name = "increment", strategy = "increment")
115+
private Integer id;
116+
117+
@OrderColumn
118+
@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.EAGER)
119+
private List<Input> inputs1;
120+
121+
@OrderColumn
122+
@ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST}, fetch = FetchType.EAGER)
123+
private List<Input> inputs2;
124+
125+
}
126+
127+
@Entity
128+
@Table(name = "input")
129+
public class Input implements Serializable {
130+
@Id
131+
@GeneratedValue(strategy = GenerationType.AUTO, generator = "increment")
132+
@GenericGenerator(name = "increment", strategy = "increment")
133+
private Integer id;
134+
}
135+
}

0 commit comments

Comments
 (0)