-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HHH-13959 - Add nullability and uniqueness for @OneToOne with @JoinTable #3358
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
236 changes: 236 additions & 0 deletions
236
...rc/test/java/org/hibernate/test/annotations/onetoone/OneToOneJoinTableUniquenessTest.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,236 @@ | ||
/* | ||
* 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>. | ||
*/ | ||
|
||
/* | ||
* 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.annotations.onetoone; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.JoinTable; | ||
import javax.persistence.OneToOne; | ||
import javax.persistence.Table; | ||
|
||
import org.hibernate.cfg.AvailableSettings; | ||
import org.hibernate.cfg.Configuration; | ||
import org.hibernate.dialect.H2Dialect; | ||
|
||
import org.hibernate.testing.RequiresDialect; | ||
import org.hibernate.testing.TestForIssue; | ||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; | ||
import org.junit.After; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
@TestForIssue(jiraKey = "HHH-13959") | ||
public class OneToOneJoinTableUniquenessTest extends BaseCoreFunctionalTestCase { | ||
File output; | ||
|
||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class[] { Profile.class, PersonRole.class }; | ||
} | ||
|
||
@Override | ||
protected void configure(Configuration configuration) { | ||
|
||
try { | ||
output = File.createTempFile( "update_script", ".sql" ); | ||
} | ||
catch (IOException e) { | ||
e.printStackTrace(); | ||
fail( e.getMessage() ); | ||
} | ||
String value = output.toPath().toString(); | ||
configuration.setProperty( AvailableSettings.HBM2DDL_SCRIPTS_CREATE_TARGET, value ); | ||
configuration.setProperty( AvailableSettings.HBM2DDL_SCRIPTS_ACTION, "create" ); | ||
configuration.setProperty( AvailableSettings.HBM2DDL_DATABASE_ACTION, "create-drop" ); | ||
configuration.setProperty( AvailableSettings.FORMAT_SQL, "false" ); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
inTransaction( | ||
session -> { | ||
session.createQuery( "delete from PersonRole" ).executeUpdate(); | ||
session.createQuery( "delete from Profile" ).executeUpdate(); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
@RequiresDialect(H2Dialect.class) | ||
public void testJoinTableColumnAreBothNotNull() throws Exception { | ||
List<String> commands = Files.readAllLines( output.toPath() ); | ||
boolean isJoinTableCreated = false; | ||
for ( String command : commands ) { | ||
String lowerCaseCommand = command.toLowerCase(); | ||
if ( lowerCaseCommand.contains( "create table profile_person_role" ) ) { | ||
isJoinTableCreated = true; | ||
assertTrue( lowerCaseCommand.contains( "personrole_roleid bigint not null" ) ); | ||
assertTrue( lowerCaseCommand.contains( "id bigint not null" ) ); | ||
} | ||
} | ||
assertTrue( "The Join table was not created", isJoinTableCreated ); | ||
} | ||
|
||
@Test | ||
public void testPersistProfile() { | ||
inTransaction( | ||
session -> { | ||
Profile p = new Profile(); | ||
session.persist( p ); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testPersistPersonRole() { | ||
inTransaction( | ||
session -> { | ||
PersonRole personRole = new PersonRole(); | ||
session.persist( personRole ); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testPersistBothSameTime() { | ||
inTransaction( | ||
session -> { | ||
PersonRole personRole = new PersonRole(); | ||
Profile profile = new Profile(); | ||
profile.addPersonalRole( personRole ); | ||
|
||
session.persist( personRole ); | ||
session.persist( profile ); | ||
} | ||
); | ||
} | ||
|
||
@Test | ||
public void testPersistBothAndAssociateLater() { | ||
inTransaction( | ||
session -> { | ||
PersonRole personRole = new PersonRole(); | ||
Profile profile = new Profile(); | ||
session.persist( personRole ); | ||
|
||
session.persist( profile ); | ||
|
||
session.flush(); | ||
|
||
profile.addPersonalRole( personRole ); | ||
} | ||
); | ||
} | ||
|
||
@Entity(name = "Profile") | ||
@Table(name = "profile") | ||
public class Profile { | ||
protected Long id; | ||
|
||
String name; | ||
|
||
PersonRole personRole; | ||
|
||
public Profile() { | ||
} | ||
|
||
@Id | ||
@GeneratedValue | ||
public Long getId() { | ||
return this.id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
@OneToOne(fetch = FetchType.LAZY) | ||
@JoinTable( | ||
name = "profile_person_role", | ||
inverseJoinColumns = { @JoinColumn(unique = true, nullable = false) } | ||
) | ||
public PersonRole getPersonRole() { | ||
return this.personRole; | ||
} | ||
|
||
public void setPersonRole(PersonRole personRole) { | ||
this.personRole = personRole; | ||
} | ||
|
||
public void addPersonalRole(PersonRole personRole) { | ||
this.personRole = personRole; | ||
personRole.setProfile( this ); | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
@Entity(name = "PersonRole") | ||
@Table(name = "person_role") | ||
public class PersonRole { | ||
Long roleId; | ||
|
||
String email; | ||
|
||
Profile profile; | ||
|
||
public PersonRole() { | ||
} | ||
|
||
@Id | ||
@Column(name = "roleid") | ||
@GeneratedValue | ||
public Long getRoleId() { | ||
return this.roleId; | ||
} | ||
|
||
public void setRoleId(Long roleId) { | ||
this.roleId = roleId; | ||
} | ||
|
||
@Column(name = "email") | ||
public String getEmail() { | ||
return this.email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
@OneToOne(mappedBy = "personRole", fetch = FetchType.LAZY) | ||
public Profile getProfile() { | ||
return this.profile; | ||
} | ||
|
||
public void setProfile(Profile profile) { | ||
this.profile = profile; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand you wanna avoid a temporary variable for
makePropertyAndBind()
result, but it is still a little bit confusing whypropertyBinder.makePropertyAndBind()
remains the same across the two if blocks.Maybe the following refactoring is a little bit better in terms of code readability
Not a big deal and feel free to ignore my comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @NathanQingyangXu , I have applied your suggestion.