Fix MappingModelCreationHelper#interpretToOneKeyDescriptor() throwing NotYetImplementedFor6Exception for NonAggregateIdentifier

This commit is contained in:
Andrea Boriero 2021-11-04 09:37:42 +01:00 committed by Christian Beikov
parent d33200b309
commit 55b1ec48f1
15 changed files with 506 additions and 491 deletions

View File

@ -1063,6 +1063,10 @@ public class MappingModelCreationHelper {
);
attributeMapping.setForeignKeyDescriptor( embeddedForeignKeyDescriptor );
}
else if ( modelPart == null ) {
throw new IllegalArgumentException( "Unable to find attribute " + bootProperty.getPersistentClass()
.getEntityName() + " -> " + bootProperty.getName() );
}
else {
throw new NotYetImplementedFor6Exception(
"Support for foreign-keys based on `" + modelPart + "` not yet implemented: " +

View File

@ -4,7 +4,7 @@
* 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.derivedidentities.bidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.bidirectional;
import java.io.Serializable;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

View File

@ -4,7 +4,7 @@
* 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.derivedidentities.bidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.bidirectional;
import java.io.Serializable;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;

View File

@ -4,7 +4,7 @@
* 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.derivedidentities.bidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.bidirectional;
/**

View File

@ -0,0 +1,62 @@
/*
* 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.derivedidentities.bidirectional;
import org.hibernate.Session;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.orm.test.util.SchemaUtil;
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;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Hardy Ferentschik
*/
@DomainModel(
annotatedClasses = {
Dependent.class,
Employee.class
}
)
@SessionFactory
public class DerivedIdentityWithBidirectionalAssociationTest {
@Test
public void testBidirectionalAssociation(SessionFactoryScope scope) {
final MetadataImplementor metadata = scope.getMetadataImplementor();
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "emp_empId", metadata ) );
assertTrue( !SchemaUtil.isColumnPresent( "Dependent", "empPK", metadata ) );
Employee e = new Employee();
e.empId = 1;
e.empName = "Emmanuel";
scope.inTransaction(
session -> {
session.persist( e );
Dependent d = new Dependent();
d.emp = e;
session.persist( d );
session.flush();
session.clear();
d = getDerivedClassById( e, session, Dependent.class );
assertEquals( e.empId, d.emp.empId );
}
);
}
private <T> T getDerivedClassById(Employee e, Session s, Class<T> clazz) {
return clazz.cast( s.createQuery( "from " + clazz.getName() + " d where d.emp.empId = :empId" )
.setParameter( "empId", e.empId ).uniqueResult() );
}
}

View File

@ -4,7 +4,7 @@
* 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.derivedidentities.bidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.bidirectional;
import java.util.Set;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;

View File

@ -4,7 +4,7 @@
* 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.derivedidentities.bidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.bidirectional;
import java.io.Serializable;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;

View File

@ -4,9 +4,21 @@
* 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.derivedidentities.bidirectional;
package org.hibernate.orm.test.annotations.derivedidentities.bidirectional;
import java.io.Serializable;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.testing.TestForIssue;
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.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
@ -15,27 +27,23 @@ import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class OneToOneLazyDerivedIdFetchModeSelectTest extends BaseCoreFunctionalTestCase {
@DomainModel(
annotatedClasses = {
OneToOneLazyDerivedIdFetchModeSelectTest.Foo.class,
OneToOneLazyDerivedIdFetchModeSelectTest.Bar.class,
}
)
@SessionFactory
public class OneToOneLazyDerivedIdFetchModeSelectTest {
private Foo foo;
@Test
@TestForIssue( jiraKey = "HHH-14390")
public void testQuery() {
doInHibernate( this::sessionFactory, session -> {
@TestForIssue(jiraKey = "HHH-14390")
public void testQuery(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo.id = :id" )
.setParameter( "id", foo.getId() )
.uniqueResult();
@ -43,14 +51,13 @@ public class OneToOneLazyDerivedIdFetchModeSelectTest extends BaseCoreFunctional
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Test
@TestForIssue(jiraKey = "HHH-14390")
public void testQueryById() {
doInHibernate( this::sessionFactory, session -> {
public void testQueryById(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo = :foo" )
.setParameter( "foo", foo )
.uniqueResult();
@ -58,25 +65,24 @@ public class OneToOneLazyDerivedIdFetchModeSelectTest extends BaseCoreFunctional
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Test
@TestForIssue(jiraKey = "HHH-14390")
public void testFindByPrimaryKey() {
doInHibernate( this::sessionFactory, session -> {
public void testFindByPrimaryKey(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Bar newBar = session.find( Bar.class, foo.getId() );
assertNotNull( newBar );
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
});
} );
}
@Before
public void setupData() {
this.foo = doInHibernate( this::sessionFactory, session -> {
@BeforeEach
public void setupData(SessionFactoryScope scope) {
this.foo = scope.fromTransaction( session -> {
Foo foo = new Foo();
session.persist( foo );
@ -94,24 +100,16 @@ public class OneToOneLazyDerivedIdFetchModeSelectTest extends BaseCoreFunctional
assertEquals( foo.getId(), bar.getFoo().getId() );
return foo;
});
} );
}
@After
public void cleanupData() {
@AfterEach
public void cleanupData(SessionFactoryScope scope) {
this.foo = null;
doInHibernate( this::sessionFactory, session -> {
scope.inTransaction( session -> {
session.createQuery( "delete from Bar" );
session.createQuery( "delete from Foo" );
});
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Foo.class,
Bar.class,
};
} );
}
@Entity(name = "Foo")

View File

@ -0,0 +1,257 @@
/*
* 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.derivedidentities.bidirectional;
import java.util.List;
import org.hibernate.query.Query;
import org.hibernate.testing.TestForIssue;
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.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@DomainModel(
annotatedClasses = {
Foo.class,
Bar.class,
Person.class,
PersonInfo.class
}
)
@SessionFactory
public class OneToOneWithDerivedIdentityTest {
@AfterEach
public void tearDown(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
session.createQuery( "delete from Bar" ).executeUpdate();
session.createQuery( "delete from Foo" ).executeUpdate();
session.createQuery( "delete from PersonInfo" ).executeUpdate();
session.createQuery( "delete from Person" ).executeUpdate();
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-11903")
public void testInsertFooAndBarWithDerivedId(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
session.persist( foo );
session.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
session.clear();
Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo.id = :id" )
.setParameter( "id", foo.getId() )
.uniqueResult();
assertNotNull( newBar );
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-14389")
public void testQueryById(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
session.persist( foo );
session.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
session.clear();
Bar newBar = (Bar) session.createQuery( "SELECT b FROM Bar b WHERE b.foo = :foo" )
.setParameter( "foo", foo )
.uniqueResult();
assertNotNull( newBar );
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-14389")
public void testFindById(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
session.persist( foo );
session.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
session.clear();
try {
session.find( Bar.class, foo );
fail( "Should have thrown IllegalArgumentException" );
}
catch (IllegalArgumentException expected) {
}
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-14389")
public void testFindByPrimaryKey(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
session.persist( foo );
session.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
session.clear();
Bar newBar = session.find( Bar.class, foo.getId() );
assertNotNull( newBar );
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-10476")
public void testInsertFooAndBarWithDerivedIdPC(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
session.persist( foo );
session.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
session.clear();
Bar barWithFoo = new Bar();
barWithFoo.setFoo( foo );
barWithFoo.setDetails( "wrong details" );
bar = session.get( Bar.class, barWithFoo );
assertSame( bar, barWithFoo );
assertEquals( "Some details", bar.getDetails() );
assertTrue( session.getPersistenceContext().isEntryFor( bar ) );
assertFalse( session.getPersistenceContext().isEntryFor( bar.getFoo() ) );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-6813")
public void testSelectWithDerivedId(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
session.persist( foo );
session.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
session.clear();
Foo newFoo = (Foo) session.createQuery( "SELECT f FROM Foo f" ).uniqueResult();
assertNotNull( newFoo );
assertNotNull( newFoo.getBar() );
assertSame( newFoo, newFoo.getBar().getFoo() );
assertEquals( "Some details", newFoo.getBar().getDetails() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-6813")
// Regression test utilizing multiple types of queries.
public void testCase(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Person p = new Person();
p.setName( "Alfio" );
PersonInfo pi = new PersonInfo();
pi.setId( p );
pi.setInfo( "Some information" );
session.persist( p );
session.persist( pi );
session.getTransaction().commit();
session.clear();
session.getTransaction().begin();
Query q = session.getNamedQuery( "PersonQuery" );
List<Person> persons = q.list();
assertEquals( persons.size(), 1 );
assertEquals( persons.get( 0 ).getName(), "Alfio" );
session.getTransaction().commit();
session.clear();
session.getTransaction().begin();
p = session.get( Person.class, persons.get( 0 ).getId() );
assertEquals( p.getName(), "Alfio" );
}
);
}
}

View File

@ -0,0 +1,76 @@
/*
* 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.derivedidentities.bidirectional;
import java.io.Serializable;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.OneToOne;
@Entity
@NamedQuery(name = "PersonQuery", query = "SELECT p FROM Person p")
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Basic
private String name;
@OneToOne(mappedBy = "id")
private PersonInfo personInfo;
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int hashCode() {
int hash = 0;
hash += ( this.id != null ? this.id.hashCode() : 0 );
return hash;
}
public boolean equals(Object object) {
if ( !( object instanceof Person ) ) {
return false;
}
Person other = (Person) object;
return ( ( this.id != null ) || ( other.id == null ) ) && ( ( this.id == null ) || ( this.id.equals( other.id ) ) );
}
public String toString() {
return "nogroup.hibertest.Person[ id=" + this.id + " ]";
}
public PersonInfo getPersonInfo() {
return this.personInfo;
}
public void setPersonInfo(PersonInfo personInfo) {
this.personInfo = personInfo;
}
}

View File

@ -0,0 +1,61 @@
/*
* 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.derivedidentities.bidirectional;
import java.io.Serializable;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
@Entity
public class PersonInfo implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@OneToOne
private Person id;
@Basic
private String info;
public Person getId() {
return this.id;
}
public void setId(Person id) {
this.id = id;
}
public String getInfo() {
return this.info;
}
public void setInfo(String info) {
this.info = info;
}
public int hashCode() {
int hash = 0;
hash += ( this.id != null ? this.id.hashCode() : 0 );
return hash;
}
public boolean equals(Object object) {
if ( !( object instanceof PersonInfo ) ) {
return false;
}
PersonInfo other = (PersonInfo) object;
return ( ( this.id != null ) || ( other.id == null ) ) && ( ( this.id == null ) || ( this.id.equals( other.id ) ) );
}
public String toString() {
return "nogroup.hibertest.PersonInfo[ id=" + this.id + " ]";
}
}

View File

@ -1,58 +0,0 @@
/*
* 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.derivedidentities.bidirectional;
import org.hibernate.Session;
import org.hibernate.testing.Skip;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.hibernate.orm.test.util.SchemaUtil;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Hardy Ferentschik
*/
@Skip( condition = Skip.AlwaysSkip.class,message = "sdf")
public class DerivedIdentityWithBidirectionalAssociationTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
public void testBidirectionalAssociation() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "emp_empId", metadata() ) );
assertTrue( !SchemaUtil.isColumnPresent( "Dependent", "empPK", metadata() ) );
Employee e = new Employee();
e.empId = 1;
e.empName = "Emmanuel";
Session s = openSession();
s.getTransaction().begin();
s.persist( e );
Dependent d = new Dependent();
d.emp = e;
s.persist( d );
s.flush();
s.clear();
d = getDerivedClassById( e, s, Dependent.class );
assertEquals( e.empId, d.emp.empId );
s.getTransaction().rollback();
s.close();
}
private <T> T getDerivedClassById(Employee e, Session s, Class<T> clazz) {
return clazz.cast( s.createQuery( "from " + clazz.getName() + " d where d.emp.empId = :empId" )
.setParameter( "empId", e.empId ).uniqueResult() );
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Dependent.class,
Employee.class
};
}
}

View File

@ -1,236 +0,0 @@
/*
* 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.derivedidentities.bidirectional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.List;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
public class OneToOneWithDerivedIdentityTest extends BaseCoreFunctionalTestCase {
@Test
@TestForIssue(jiraKey = "HHH-11903")
public void testInsertFooAndBarWithDerivedId() {
Session s = openSession();
s.beginTransaction();
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
s.persist( foo );
s.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
s.clear();
Bar newBar = ( Bar ) s.createQuery( "SELECT b FROM Bar b WHERE b.foo.id = :id" )
.setParameter( "id", foo.getId() )
.uniqueResult();
assertNotNull( newBar );
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
s.getTransaction().rollback();
s.close();
}
@Test
@TestForIssue(jiraKey = "HHH-14389")
public void testQueryById() {
Session s = openSession();
s.beginTransaction();
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
s.persist( foo );
s.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
s.clear();
Bar newBar = ( Bar ) s.createQuery( "SELECT b FROM Bar b WHERE b.foo = :foo" )
.setParameter( "foo", foo )
.uniqueResult();
assertNotNull( newBar );
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
s.getTransaction().rollback();
s.close();
}
@Test
@TestForIssue(jiraKey = "HHH-14389")
public void testFindById() {
Session s = openSession();
s.beginTransaction();
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
s.persist( foo );
s.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
s.clear();
try {
s.find( Bar.class, foo );
fail( "Should have thrown IllegalArgumentException" );
}
catch(IllegalArgumentException expected) {
}
finally {
s.getTransaction().rollback();
}
s.close();
}
@Test
@TestForIssue(jiraKey = "HHH-14389")
public void testFindByPrimaryKey() {
Session s = openSession();
s.beginTransaction();
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
s.persist( foo );
s.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
s.clear();
Bar newBar = s.find( Bar.class, foo.getId() );
assertNotNull( newBar );
assertNotNull( newBar.getFoo() );
assertEquals( foo.getId(), newBar.getFoo().getId() );
assertEquals( "Some details", newBar.getDetails() );
s.getTransaction().rollback();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-10476")
public void testInsertFooAndBarWithDerivedIdPC() {
Session s = openSession();
s.beginTransaction();
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
s.persist( foo );
s.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
s.clear();
Bar barWithFoo = new Bar();
barWithFoo.setFoo( foo );
barWithFoo.setDetails( "wrong details" );
bar = (Bar) s.get( Bar.class, barWithFoo );
assertSame( bar, barWithFoo );
assertEquals( "Some details", bar.getDetails() );
SessionImplementor si = (SessionImplementor) s;
assertTrue( si.getPersistenceContext().isEntryFor( bar ) );
assertFalse( si.getPersistenceContext().isEntryFor( bar.getFoo() ) );
s.getTransaction().rollback();
s.close();
}
@Test
@TestForIssue(jiraKey = "HHH-6813")
public void testSelectWithDerivedId() {
Session s = openSession();
s.beginTransaction();
Bar bar = new Bar();
bar.setDetails( "Some details" );
Foo foo = new Foo();
foo.setBar( bar );
bar.setFoo( foo );
s.persist( foo );
s.flush();
assertNotNull( foo.getId() );
assertEquals( foo.getId(), bar.getFoo().getId() );
s.clear();
Foo newFoo = (Foo) s.createQuery( "SELECT f FROM Foo f" ).uniqueResult();
assertNotNull( newFoo );
assertNotNull( newFoo.getBar() );
assertSame( newFoo, newFoo.getBar().getFoo() );
assertEquals( "Some details", newFoo.getBar().getDetails() );
s.getTransaction().rollback();
s.close();
}
@Test
@TestForIssue(jiraKey = "HHH-6813")
// Regression test utilizing multiple types of queries.
public void testCase() {
Session s = openSession();
s.getTransaction().begin();
Person p = new Person();
p.setName( "Alfio" );
PersonInfo pi = new PersonInfo();
pi.setId( p );
pi.setInfo( "Some information" );
s.persist( p );
s.persist( pi );
s.getTransaction().commit();
s.clear();
s.getTransaction().begin();
Query q = s.getNamedQuery( "PersonQuery" );
List<Person> persons = q.list();
assertEquals( persons.size(), 1 );
assertEquals( persons.get( 0 ).getName(), "Alfio" );
s.getTransaction().commit();
s.clear();
s.getTransaction().begin();
p = (Person) s.get( Person.class, persons.get( 0 ).getId() );
assertEquals( p.getName(), "Alfio" );
s.getTransaction().commit();
s.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Foo.class,
Bar.class,
Person.class,
PersonInfo.class
};
}
}

View File

@ -1,83 +0,0 @@
/*
* 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.derivedidentities.bidirectional;
import java.io.Serializable;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.OneToOne;
@Entity
@NamedQuery(name="PersonQuery", query="SELECT p FROM Person p")
public class Person
implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Basic
private String name;
@OneToOne(mappedBy="id")
private PersonInfo personInfo;
public Integer getId()
{
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int hashCode()
{
int hash = 0;
hash += (this.id != null ? this.id.hashCode() : 0);
return hash;
}
public boolean equals(Object object)
{
if (!(object instanceof Person)) {
return false;
}
Person other = (Person)object;
return ((this.id != null) || (other.id == null)) && ((this.id == null) || (this.id.equals(other.id)));
}
public String toString()
{
return "nogroup.hibertest.Person[ id=" + this.id + " ]";
}
public PersonInfo getPersonInfo()
{
return this.personInfo;
}
public void setPersonInfo(PersonInfo personInfo)
{
this.personInfo = personInfo;
}
}

View File

@ -1,66 +0,0 @@
/*
* 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.derivedidentities.bidirectional;
import java.io.Serializable;
import jakarta.persistence.Basic;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
@Entity
public class PersonInfo
implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@OneToOne
private Person id;
@Basic
private String info;
public Person getId()
{
return this.id;
}
public void setId(Person id) {
this.id = id;
}
public String getInfo() {
return this.info;
}
public void setInfo(String info) {
this.info = info;
}
public int hashCode()
{
int hash = 0;
hash += (this.id != null ? this.id.hashCode() : 0);
return hash;
}
public boolean equals(Object object)
{
if (!(object instanceof PersonInfo)) {
return false;
}
PersonInfo other = (PersonInfo)object;
return ((this.id != null) || (other.id == null)) && ((this.id == null) || (this.id.equals(other.id)));
}
public String toString()
{
return "nogroup.hibertest.PersonInfo[ id=" + this.id + " ]";
}
}