mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-27 22:39:13 +00:00
HHH-8573: Test case illustrating creation of a superclass instance when
subclass instance needed
This commit is contained in:
parent
202e46eef2
commit
3a75f9fb4e
@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Inc.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
package org.hibernate.test.lazynocascadecache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vasily Kochnev
|
||||||
|
*/
|
||||||
|
public class BaseChild {
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private BaseChild dependency;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Entity identifier.
|
||||||
|
*/
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param id Identifier to set.
|
||||||
|
*/
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Child dependency
|
||||||
|
*/
|
||||||
|
public BaseChild getDependency() {
|
||||||
|
return dependency;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param dependency Dependency to set.
|
||||||
|
*/
|
||||||
|
public void setDependency(BaseChild dependency) {
|
||||||
|
this.dependency = dependency;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Inc.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
package org.hibernate.test.lazynocascadecache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vasily Kochnev
|
||||||
|
*/
|
||||||
|
public class Child extends BaseChild {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Name of the child.
|
||||||
|
*/
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param name The name to set.
|
||||||
|
*/
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Inc.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
package org.hibernate.test.lazynocascadecache;
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.Transaction;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vasily Kochnev
|
||||||
|
*/
|
||||||
|
public class LazyNoCascadeCacheTest extends BaseCoreFunctionalTestCase {
|
||||||
|
public String[] getMappings() {
|
||||||
|
return new String[] {"lazynocascadecache/Parent.hbm.xml"};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoCascadeCache() {
|
||||||
|
Parent parent = new Parent();
|
||||||
|
|
||||||
|
BaseChild firstChild = new BaseChild();
|
||||||
|
parent.getChildren().add( firstChild );
|
||||||
|
|
||||||
|
Child secondChild = new Child();
|
||||||
|
secondChild.setName( "SecondChildName" );
|
||||||
|
parent.getChildren().add( secondChild );//Dependency child must go after dependent in collection
|
||||||
|
|
||||||
|
firstChild.setDependency( secondChild );
|
||||||
|
|
||||||
|
Session s = openSession();
|
||||||
|
Transaction t = s.beginTransaction();
|
||||||
|
s.merge( parent );
|
||||||
|
t.commit();
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<!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.lazynocascadecache">
|
||||||
|
<class name="Parent">
|
||||||
|
<id name="id" column="parent_id">
|
||||||
|
<generator class="increment"/>
|
||||||
|
</id>
|
||||||
|
<set name="children" cascade="all">
|
||||||
|
<key column="parent_id"/>
|
||||||
|
<one-to-many class="BaseChild"/>
|
||||||
|
</set>
|
||||||
|
</class>
|
||||||
|
|
||||||
|
<class name="BaseChild" >
|
||||||
|
<id name="id" column="base_child_id">
|
||||||
|
<generator class="increment"/>
|
||||||
|
</id>
|
||||||
|
<discriminator/>
|
||||||
|
<many-to-one name="dependency" column="dependency_id" class="BaseChild" lazy="proxy"/>
|
||||||
|
<subclass name="Child">
|
||||||
|
<property name="name" column="name" type="string"/>
|
||||||
|
</subclass>
|
||||||
|
</class>
|
||||||
|
</hibernate-mapping>
|
@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Hibernate, Relational Persistence for Idiomatic Java
|
||||||
|
*
|
||||||
|
* Copyright (c) 2013, Red Hat Inc. or third-party contributors as
|
||||||
|
* indicated by the @author tags or express copyright attribution
|
||||||
|
* statements applied by the authors. All third-party contributions are
|
||||||
|
* distributed under license by Red Hat Inc.
|
||||||
|
*
|
||||||
|
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||||
|
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||||
|
* Lesser General Public License, as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||||
|
* for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this distribution; if not, write to:
|
||||||
|
* Free Software Foundation, Inc.
|
||||||
|
* 51 Franklin Street, Fifth Floor
|
||||||
|
* Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
package org.hibernate.test.lazynocascadecache;
|
||||||
|
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vasily Kochnev
|
||||||
|
*/
|
||||||
|
public class Parent {
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
// LinkedHashSet used for the reason to force the specific order of elements in collection
|
||||||
|
private Set<BaseChild> children = new LinkedHashSet<BaseChild>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Entity identifier.
|
||||||
|
*/
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param id Identifier to set.
|
||||||
|
*/
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Set of children entities.
|
||||||
|
*/
|
||||||
|
public Set<BaseChild> getChildren() {
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param children Set of children entities to set.
|
||||||
|
*/
|
||||||
|
public void setChildren(Set<BaseChild> children) {
|
||||||
|
this.children = children;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user