clean up remaining test suite failures
This commit is contained in:
parent
1dcdec9c15
commit
ba024c5475
|
@ -1,57 +0,0 @@
|
|||
<?xml version="1.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>.
|
||||
-->
|
||||
<!DOCTYPE hibernate-mapping PUBLIC
|
||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<hibernate-mapping package="org.hibernate.test.dynamicentity">
|
||||
|
||||
<!--
|
||||
Mapping the Customer and Company interfaces. Our custom Interceptor
|
||||
will be responsible for: a) creating instances representing these
|
||||
interfaces; b) determining the appropriate entity-name (i.e., which
|
||||
entity mapping to use) given an instance of one of these proxies.
|
||||
-->
|
||||
<class name="Person" table="t_person" abstract="false">
|
||||
<!-- <class name="Person" table="t_person" discriminator-value="person"> -->
|
||||
<id name="id">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
<discriminator column="t_person_dis" force="false"/>
|
||||
<property name="name"/>
|
||||
|
||||
<many-to-one name="address" cascade="all" column="addr_id"/>
|
||||
|
||||
<set name="family" lazy="true" cascade="all">
|
||||
<key column="pers_id"/>
|
||||
<one-to-many class="Person"/>
|
||||
</set>
|
||||
|
||||
<subclass name="Customer" discriminator-value="customer" abstract="false">
|
||||
<many-to-one name="company" cascade="none" column="comp_id"/>
|
||||
</subclass>
|
||||
</class>
|
||||
|
||||
<!-- Company interface mapping -->
|
||||
<class name="Company" table="t_company" abstract="false">
|
||||
<id name="id">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
<property name="name"/>
|
||||
</class>
|
||||
|
||||
<class name="Address" table="t_address" abstract="false">
|
||||
<id name="id">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
<property name="street"/>
|
||||
<property name="city"/>
|
||||
<property name="postalCode"/>
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
|
@ -1,117 +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.orm.test.dynamicentity.interceptor;
|
||||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.boot.SessionFactoryBuilder;
|
||||
|
||||
import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest;
|
||||
import org.hibernate.test.dynamicentity.Company;
|
||||
import org.hibernate.test.dynamicentity.Customer;
|
||||
import org.hibernate.test.dynamicentity.ProxyHelper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* Demonstrates custom interpretation of entity-name through
|
||||
* an Interceptor.
|
||||
* <p/>
|
||||
* Here, we are generating dynamic
|
||||
* {@link java.lang.reflect.Proxy proxies} on the fly to represent
|
||||
* our entities. Because of this, Hibernate would not be able to
|
||||
* determine the appropriate entity mapping to use given one of
|
||||
* these proxies (they are named like $Proxy1, or such). Thus, we
|
||||
* plug a custom Interceptor into the session to perform this
|
||||
* entity-name interpretation.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
* @see ProxyInterceptor
|
||||
*/
|
||||
public class InterceptorDynamicEntityTest extends BaseSessionFactoryFunctionalTest {
|
||||
|
||||
@Override
|
||||
protected String[] getOrmXmlFiles() {
|
||||
return new String[] { "org/hibernate/orm/test/dynamicentity/interceptor/Customer.hbm.xml" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(SessionFactoryBuilder builder) {
|
||||
builder.applyInterceptor( new ProxyInterceptor() );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIt() {
|
||||
// Test saving these dyna-proxies
|
||||
Company company = ProxyHelper.newCompanyProxy();
|
||||
Long customerId = fromTransaction(
|
||||
session -> {
|
||||
Customer customer = ProxyHelper.newCustomerProxy();
|
||||
company.setName( "acme" );
|
||||
session.save( company );
|
||||
customer.setName( "Steve" );
|
||||
customer.setCompany( company );
|
||||
session.save( customer );
|
||||
return customer.getId();
|
||||
}
|
||||
);
|
||||
|
||||
assertNotNull( company.getId(), "company id not assigned" );
|
||||
assertNotNull( customerId, "customer id not assigned" );
|
||||
|
||||
// Test loading these dyna-proxies, along with flush processing
|
||||
Customer customer = fromTransaction(
|
||||
session -> {
|
||||
Customer c = session.load( Customer.class, customerId );
|
||||
assertFalse( Hibernate.isInitialized( c ), "should-be-proxy was initialized" );
|
||||
|
||||
c.setName( "other" );
|
||||
session.flush();
|
||||
assertFalse( Hibernate.isInitialized( c.getCompany() ), "should-be-proxy was initialized" );
|
||||
|
||||
session.refresh( c );
|
||||
assertEquals( "other", c.getName(), "name not updated" );
|
||||
assertEquals( "acme", c.getCompany().getName(), "company association not correct" );
|
||||
return c;
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
// Test detached entity re-attachment with these dyna-proxies
|
||||
customer.setName( "Steve" );
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.update( customer );
|
||||
session.flush();
|
||||
session.refresh( customer );
|
||||
assertEquals( "Steve", customer.getName(), "name not updated" );
|
||||
}
|
||||
);
|
||||
|
||||
// Test querying
|
||||
inTransaction(
|
||||
session -> {
|
||||
int count = session.createQuery( "from Customer" ).list().size();
|
||||
assertEquals( 1, count, "querying dynamic entity" );
|
||||
session.clear();
|
||||
count = session.createQuery( "from Person" ).list().size();
|
||||
assertEquals( 1, count, "querying dynamic entity" );
|
||||
}
|
||||
);
|
||||
|
||||
// test deleteing
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.delete( company );
|
||||
session.delete( customer );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,73 +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.orm.test.dynamicentity.interceptor;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
import org.hibernate.EmptyInterceptor;
|
||||
import org.hibernate.metamodel.RepresentationMode;
|
||||
|
||||
import org.hibernate.test.dynamicentity.Company;
|
||||
import org.hibernate.test.dynamicentity.Customer;
|
||||
import org.hibernate.test.dynamicentity.ProxyHelper;
|
||||
|
||||
/**
|
||||
* Our custom {@link org.hibernate.Interceptor} impl which performs the
|
||||
* interpretation of entity-name -> proxy instance and vice-versa.
|
||||
*
|
||||
* @author <a href="mailto:steve@hibernate.org">Steve Ebersole </a>
|
||||
*/
|
||||
public class ProxyInterceptor extends EmptyInterceptor {
|
||||
|
||||
/**
|
||||
* The callback from Hibernate to determine the entity name given
|
||||
* a presumed entity instance.
|
||||
*
|
||||
* @param object The presumed entity instance.
|
||||
*
|
||||
* @return The entity name (pointing to the proper entity mapping).
|
||||
*/
|
||||
@Override
|
||||
public String getEntityName(Object object) {
|
||||
String entityName = ProxyHelper.extractEntityName( object );
|
||||
if ( entityName == null ) {
|
||||
entityName = super.getEntityName( object );
|
||||
}
|
||||
return entityName;
|
||||
}
|
||||
|
||||
/**
|
||||
* The callback from Hibernate in order to build an instance of the
|
||||
* entity represented by the given entity name. Here, we build a
|
||||
* {@link Proxy} representing the entity.
|
||||
*
|
||||
* @param entityName The entity name for which to create an instance. In our setup,
|
||||
* this is the interface name.
|
||||
* @param entityMode The entity mode in which to create an instance. Here, we are only
|
||||
* interestes in custom behavior for the POJO entity mode.
|
||||
* @param id The identifier value for the given entity.
|
||||
*
|
||||
* @return The instantiated instance.
|
||||
*/
|
||||
@Override
|
||||
public Object instantiate(
|
||||
String entityName,
|
||||
RepresentationMode entityMode,
|
||||
Object id) {
|
||||
if ( entityMode == RepresentationMode.POJO ) {
|
||||
if ( Customer.class.getName().equals( entityName ) ) {
|
||||
return ProxyHelper.newCustomerProxy( id );
|
||||
}
|
||||
else if ( Company.class.getName().equals( entityName ) ) {
|
||||
return ProxyHelper.newCompanyProxy( id );
|
||||
}
|
||||
}
|
||||
return super.instantiate( entityName, entityMode, id );
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue