HHH-4797 Ignore backref properties for JPA 2 model

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18561 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2010-01-15 10:47:18 +00:00
parent 56f6941260
commit c8c9263918
5 changed files with 111 additions and 5 deletions

View File

@ -73,10 +73,12 @@ public class AttributeFactory {
* @param property The Hibernate property descriptor for the attribute
* @param <X> The type of the owner
* @param <Y> The attribute type
* @return The built attribute descriptor
* @return The built attribute descriptor or null if the attribute is not part of the JPA 2 model (eg backrefs)
*/
@SuppressWarnings({ "unchecked" })
public <X, Y> AttributeImplementor<X, Y> buildAttribute(AbstractManagedType<X> ownerType, Property property) {
//a back ref is a virtual property created by Hibernate, let's hide it from the JPA model.
if ( property.isBackRef() ) return null;
final AttributeContext<X> attributeContext = wrap( ownerType, property );
final AttributeMetadata<X,Y> attributeMetadata =
determineAttributeMetadata( attributeContext, NORMAL_MEMBER_RESOLVER );
@ -208,7 +210,10 @@ public class AttributeFactory {
final Iterator<Property> subProperties = component.getPropertyIterator();
while ( subProperties.hasNext() ) {
final Property property = subProperties.next();
embeddableType.getBuilder().addAttribute( buildAttribute( embeddableType, property) );
final AttributeImplementor<Y, Object> attribute = buildAttribute( embeddableType, property );
if ( attribute != null ) {
embeddableType.getBuilder().addAttribute( attribute );
}
}
embeddableType.lock();
return embeddableType;

View File

@ -175,7 +175,9 @@ class MetadataContext {
continue;
}
final Attribute attribute = attributeFactory.buildAttribute( jpa2Mapping, property );
jpa2Mapping.getBuilder().addAttribute( attribute );
if ( attribute != null ) {
jpa2Mapping.getBuilder().addAttribute( attribute );
}
}
jpa2Mapping.lock();
populateStaticMetamodel( jpa2Mapping );
@ -192,7 +194,9 @@ class MetadataContext {
while ( properties.hasNext() ) {
final Property property = properties.next();
final Attribute attribute = attributeFactory.buildAttribute( jpa2Mapping, property );
jpa2Mapping.getBuilder().addAttribute( attribute );
if ( attribute != null ) {
jpa2Mapping.getBuilder().addAttribute( attribute );
}
}
jpa2Mapping.lock();
populateStaticMetamodel( jpa2Mapping );

View File

@ -0,0 +1,34 @@
package org.hibernate.ejb.test.metadata;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @author Emmanuel Bernard
*/
@Entity
@Table(name="ejb_child")
public class Child {
private Integer id;
private String name;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -274,6 +274,18 @@ public class MetadataTest extends TestCase {
assertNull( thing.getSupertype() );
}
public void testOneToManyJoinColumnUnidirectional() throws Exception {
final EntityType<Parent> parent = factory.getMetamodel().entity( Parent.class );
assertNotNull( parent );
final SetAttribute<? super Parent, ?> children = parent.getSet( "children" );
assertNotNull( children );
assertEquals( 1, parent.getPluralAttributes().size() );
assertEquals( 3, parent.getAttributes().size() );
final EntityType<Child> child = factory.getMetamodel().entity( Child.class );
assertNotNull( child );
assertEquals( 2, child.getAttributes().size() );
}
private void ensureProperMember(Set<?> attributes) {
//we do not update the set so we are safe
@SuppressWarnings( "unchecked" )
@ -321,7 +333,9 @@ public class MetadataTest extends TestCase {
Feline.class,
Garden.class,
Flower.class,
JoinedManyToOneOwner.class
JoinedManyToOneOwner.class,
Parent.class,
Child.class
};
}

View File

@ -0,0 +1,49 @@
package org.hibernate.ejb.test.metadata;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* @author Emmanuel Bernard
*/
@Entity
@Table(name="ejb_parent")
public class Parent {
private Integer id;
private String name;
private Set<Child> children;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany
@JoinColumn(name="parent_fk", nullable = false)
public Set<Child> getChildren() {
return children;
}
public void setChildren(Set<Child> children) {
this.children = children;
}
}