HHH-15933 Better property owner check + new test case for JoinedSubclass

This commit is contained in:
Marco Belladelli 2023-01-23 16:59:35 +01:00 committed by Andrea Boriero
parent 49690bf4ce
commit 998f2ef21f
5 changed files with 154 additions and 3 deletions

View File

@ -336,11 +336,12 @@ public class BinderHelper {
result.setInsertable( false );
result.setValue( embeddedComponent );
result.setPropertyAccessorName( "embedded" );
if ( ownerEntity instanceof JoinedSubclass ) {
ownerEntity.addProperty( result );
if ( persistentClassOrJoin instanceof Join ) {
// the referenced column is in the joined table, add the synthetic property there
persistentClassOrJoin.addProperty( result );
}
else {
persistentClassOrJoin.addProperty( result );
ownerEntity.addProperty( result );
}
embeddedComponent.createUniqueKey(); //make it unique
return result;

View File

@ -0,0 +1,46 @@
/*
* 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.orm.test.annotations.refcolnames.subclass;
import org.hibernate.annotations.NaturalId;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
/**
* @author Marco Belladelli
*/
@Entity(name = "Animal")
@Inheritance(strategy = InheritanceType.JOINED)
public class Animal {
@Id
@GeneratedValue
private Long id;
@Column(name = "name")
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,36 @@
/*
* 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.orm.test.annotations.refcolnames.subclass;
import java.util.ArrayList;
import java.util.List;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
/**
* @author Marco Belladelli
*/
@Entity(name = "Cat")
public class Cat extends Animal {
@ManyToMany
@JoinTable(
name = "cat_toys",
joinColumns = @JoinColumn(name = "cat_name", referencedColumnName = "name")
)
private List<Toy> toys = new ArrayList<>();
public List<Toy> getToys() {
return toys;
}
public void setToys(List<Toy> toys) {
this.toys = toys;
}
}

View File

@ -0,0 +1,39 @@
/*
* 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.orm.test.annotations.refcolnames.subclass;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Marco Belladelli
*/
@SessionFactory
@DomainModel(annotatedClasses = { Animal.class, Cat.class, Toy.class })
public class RefToJoinedSuperclassTest {
@Test
public void test(SessionFactoryScope scope) {
final Cat cat = new Cat();
cat.setName( "cat" );
final Toy toy = new Toy();
cat.getToys().add( toy );
scope.inTransaction( session -> {
session.persist( cat );
session.persist( toy );
} );
scope.inSession( session -> {
Cat result = session.createQuery( "from Cat", Cat.class ).getSingleResult();
assertEquals( 1, result.getToys().size() );
assertEquals( toy.getId(), result.getToys().get( 0 ).getId() );
} );
}
}

View File

@ -0,0 +1,29 @@
/*
* 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.orm.test.annotations.refcolnames.subclass;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
/**
* @author Marco Belladelli
*/
@Entity(name = "Toy")
public class Toy {
@Id
@GeneratedValue
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}