HHH-4232 TypeDef support when used on @Embeddable or @MappedSuperClass classes

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17531 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2009-09-22 17:43:48 +00:00
parent 88caf4d958
commit 8cc9e9d5fd
5 changed files with 138 additions and 5 deletions

View File

@ -429,12 +429,13 @@ public final class AnnotationBinder {
//TODO: be more strict with secondarytable allowance (not for ids, not for secondary table join columns etc)
InheritanceState inheritanceState = inheritanceStatePerClass.get( clazzToProcess );
AnnotatedClassType classType = mappings.getClassType( clazzToProcess );
//Queries declared in MappedSuperclass should be usable in Subclasses
if ( AnnotatedClassType.EMBEDDABLE_SUPERCLASS.equals( classType )) {
bindQueries(clazzToProcess, mappings );
if ( AnnotatedClassType.EMBEDDABLE_SUPERCLASS.equals( classType ) ) {
bindQueries( clazzToProcess, mappings );
bindTypeDefs(clazzToProcess, mappings);
}
if ( AnnotatedClassType.EMBEDDABLE_SUPERCLASS.equals( classType ) //will be processed by their subentities
|| AnnotatedClassType.NONE.equals( classType ) //to be ignored
|| AnnotatedClassType.EMBEDDABLE.equals( classType ) //allow embeddable element declaration
@ -1942,6 +1943,9 @@ public final class AnnotationBinder {
);
List<PropertyData> classElements = new ArrayList<PropertyData>();
XClass returnedClassOrElement = inferredData.getClassOrElement();
//embeddable elements can have type defs
bindTypeDefs(returnedClassOrElement, mappings);
addElementsOfAClass(
classElements,
subHolder,

View File

@ -143,6 +143,33 @@ public class BasicHibernateAnnotationsTest extends TestCase {
}
//Test import of TypeDefs from MappedSuperclass and
//Embedded classes
public void testImportTypeDefinitions() throws Exception {
Name name = new Name();
name.setFirstName("SHARATH");
LastName lastName = new LastName();
lastName.setName("reddy");
name.setLastName(lastName);
Session s;
Transaction tx;
s = openSession();
tx = s.beginTransaction();
s.persist(name);
tx.commit();
s.close();
s = openSession();
tx = s.beginTransaction();
name = (Name) s.get( Name.class, name.getId() );
assertNotNull( name );
assertEquals( "sharath", name.getFirstName() );
assertEquals( "REDDY", name.getLastName().getName() );
s.delete(name);
tx.commit();
s.close();
}
public void testNonLazy() throws Exception {
Session s;
Transaction tx;
@ -325,7 +352,8 @@ public class BasicHibernateAnnotationsTest extends TestCase {
Tree.class,
Ransom.class,
ZipCode.class,
Flight.class
Flight.class,
Name.class
};
}

View File

@ -0,0 +1,33 @@
package org.hibernate.test.annotations.entity;
import javax.persistence.MappedSuperclass;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.Parameter;
@TypeDef(
name = "lowerCase",
typeClass = CasterStringType.class,
parameters = {
@Parameter(name = "cast", value = "lower")
}
)
@MappedSuperclass
public class FirstName {
@Type(type="lowerCase")
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String lowerCaseName) {
this.firstName = lowerCaseName;
}
}

View File

@ -0,0 +1,33 @@
package org.hibernate.test.annotations.entity;
import javax.persistence.Embeddable;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
@TypeDef(
name = "upperCase",
typeClass = CasterStringType.class,
parameters = {
@Parameter(name = "cast", value = "upper")
}
)
@Embeddable
public class LastName {
@Type(type="upperCase")
private String lastName;
public String getName() {
return lastName;
}
public void setName(String lowerCaseName) {
this.lastName = lowerCaseName;
}
}

View File

@ -0,0 +1,35 @@
package org.hibernate.test.annotations.entity;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;
@Entity
public class Name extends FirstName {
@Id
@GeneratedValue
private Integer id;
@Embedded
private LastName lastName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public LastName getLastName() {
return lastName;
}
public void setLastName(LastName val) {
this.lastName = val;
}
}