Skip to content

Commit 404020f

Browse files
ettavoltdreab8
authored andcommitted
HHH-4384 Allow join column override if @joincolumn is absent on @OnetoOne(mappedBy = "").
Tests: this case and disallow if mappedBy > "".
1 parent 863369c commit 404020f

File tree

2 files changed

+205
-1
lines changed

2 files changed

+205
-1
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/Ejb3JoinColumn.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ private static Ejb3JoinColumn buildJoinColumn(
253253
String suffixForDefaultColumnName,
254254
MetadataBuildingContext buildingContext) {
255255
if ( ann != null ) {
256-
if ( BinderHelper.isEmptyAnnotationValue( mappedBy ) ) {
256+
if ( !BinderHelper.isEmptyOrNullAnnotationValue( mappedBy ) ) {
257257
throw new AnnotationException(
258258
"Illegal attempt to define a @JoinColumn with a mappedBy association: "
259259
+ BinderHelper.getRelativePath( propertyHolder, propertyName )
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
package org.hibernate.test.annotations.onetoone;
2+
3+
import javax.persistence.AssociationOverride;
4+
import javax.persistence.Embeddable;
5+
import javax.persistence.Embedded;
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import javax.persistence.JoinColumn;
9+
import javax.persistence.MappedSuperclass;
10+
import javax.persistence.OneToOne;
11+
12+
import org.hibernate.AnnotationException;
13+
import org.hibernate.boot.Metadata;
14+
import org.hibernate.boot.MetadataSources;
15+
import org.hibernate.boot.model.naming.Identifier;
16+
import org.hibernate.boot.registry.StandardServiceRegistry;
17+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
18+
import org.hibernate.mapping.ForeignKey;
19+
import org.hibernate.mapping.Table;
20+
21+
import org.hibernate.testing.TestForIssue;
22+
import org.hibernate.testing.junit4.BaseUnitTestCase;
23+
import org.junit.Test;
24+
25+
import static org.junit.Assert.assertEquals;
26+
import static org.junit.Assert.assertTrue;
27+
import static org.junit.Assert.fail;
28+
29+
/**
30+
* @author Aresnii Skvortsov
31+
*/
32+
@TestForIssue(jiraKey = "HHH-4384")
33+
public class OverrideOneToOneJoinColumnTest extends BaseUnitTestCase {
34+
35+
@Test
36+
public void allowIfJoinColumnIsAbsent() {
37+
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
38+
try {
39+
Metadata metadata = new MetadataSources( ssr )
40+
.addAnnotatedClass( Person.class )
41+
.addAnnotatedClass( State.class )
42+
.buildMetadata();
43+
44+
Table personTable = metadata.getDatabase().getDefaultNamespace().locateTable( Identifier.toIdentifier(
45+
"PERSON_TABLE" ) );
46+
ForeignKey foreignKey = personTable.getForeignKeyIterator().next();
47+
48+
assertEquals(
49+
"Overridden join column name should be applied",
50+
"PERSON_ADDRESS_STATE",
51+
foreignKey.getColumn( 0 ).getName()
52+
);
53+
54+
}
55+
finally {
56+
StandardServiceRegistryBuilder.destroy( ssr );
57+
}
58+
}
59+
60+
@Test
61+
public void disallowOnSideWithMappedBy() {
62+
StandardServiceRegistry ssr = new StandardServiceRegistryBuilder().build();
63+
try {
64+
new MetadataSources( ssr )
65+
.addAnnotatedClass( Employee.class )
66+
.addAnnotatedClass( PartTimeEmployee.class )
67+
.addAnnotatedClass( Desk.class )
68+
.buildMetadata();
69+
fail( "Should disallow @JoinColumn override on side with mappedBy" );
70+
}
71+
catch (AnnotationException ex) {
72+
assertTrue(
73+
"Should disallow exactly because of @JoinColumn override on side with mappedBy",
74+
ex
75+
.getMessage()
76+
.startsWith( "Illegal attempt to define a @JoinColumn with a mappedBy association:" )
77+
);
78+
}
79+
finally {
80+
StandardServiceRegistryBuilder.destroy( ssr );
81+
}
82+
}
83+
84+
@Entity(name = "Person")
85+
@javax.persistence.Table(name = "PERSON_TABLE")
86+
public static class Person {
87+
88+
private String id;
89+
90+
private Address address;
91+
92+
@Id
93+
public String getId() {
94+
return id;
95+
}
96+
97+
public void setId(String id) {
98+
this.id = id;
99+
}
100+
101+
@Embedded
102+
@AssociationOverride(name = "state", joinColumns = { @JoinColumn(name = "PERSON_ADDRESS_STATE") })
103+
public Address getAddress() {
104+
return address;
105+
}
106+
107+
public void setAddress(Address address) {
108+
this.address = address;
109+
}
110+
}
111+
112+
@Embeddable
113+
public static class Address {
114+
115+
private String street;
116+
117+
private String city;
118+
119+
private State state;
120+
121+
@OneToOne
122+
public State getState() {
123+
return state;
124+
}
125+
126+
public void setState(State state) {
127+
this.state = state;
128+
}
129+
130+
public String getStreet() {
131+
return street;
132+
}
133+
134+
public void setStreet(String street) {
135+
this.street = street;
136+
}
137+
138+
public String getCity() {
139+
return city;
140+
}
141+
142+
public void setCity(String city) {
143+
this.city = city;
144+
}
145+
}
146+
147+
@Entity(name = "State")
148+
@javax.persistence.Table(name = "STATE_TABLE")
149+
public static class State {
150+
151+
private String id;
152+
153+
private String name;
154+
155+
@Id
156+
public String getId() {
157+
return id;
158+
}
159+
160+
public void setId(String id) {
161+
this.id = id;
162+
}
163+
164+
public String getName() {
165+
return name;
166+
}
167+
168+
public void setName(String name) {
169+
this.name = name;
170+
}
171+
}
172+
173+
@MappedSuperclass
174+
public static class Employee {
175+
176+
@Id
177+
private Long id;
178+
179+
private String name;
180+
181+
@OneToOne(mappedBy = "employee")
182+
protected Desk desk;
183+
}
184+
185+
@Entity
186+
@AssociationOverride(name = "desk",
187+
joinColumns = @JoinColumn(name = "PARTTIMEEMPLOYEE_DESK"))
188+
public static class PartTimeEmployee extends Employee {
189+
190+
}
191+
192+
@Entity(name = "Desk")
193+
public static class Desk {
194+
@Id
195+
private Long id;
196+
197+
@OneToOne
198+
private PartTimeEmployee employee;
199+
200+
private String location;
201+
}
202+
203+
204+
}

0 commit comments

Comments
 (0)