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:
parent
56f6941260
commit
c8c9263918
|
@ -73,10 +73,12 @@ public class AttributeFactory {
|
||||||
* @param property The Hibernate property descriptor for the attribute
|
* @param property The Hibernate property descriptor for the attribute
|
||||||
* @param <X> The type of the owner
|
* @param <X> The type of the owner
|
||||||
* @param <Y> The attribute type
|
* @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" })
|
@SuppressWarnings({ "unchecked" })
|
||||||
public <X, Y> AttributeImplementor<X, Y> buildAttribute(AbstractManagedType<X> ownerType, Property property) {
|
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 AttributeContext<X> attributeContext = wrap( ownerType, property );
|
||||||
final AttributeMetadata<X,Y> attributeMetadata =
|
final AttributeMetadata<X,Y> attributeMetadata =
|
||||||
determineAttributeMetadata( attributeContext, NORMAL_MEMBER_RESOLVER );
|
determineAttributeMetadata( attributeContext, NORMAL_MEMBER_RESOLVER );
|
||||||
|
@ -208,7 +210,10 @@ public class AttributeFactory {
|
||||||
final Iterator<Property> subProperties = component.getPropertyIterator();
|
final Iterator<Property> subProperties = component.getPropertyIterator();
|
||||||
while ( subProperties.hasNext() ) {
|
while ( subProperties.hasNext() ) {
|
||||||
final Property property = subProperties.next();
|
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();
|
embeddableType.lock();
|
||||||
return embeddableType;
|
return embeddableType;
|
||||||
|
|
|
@ -175,7 +175,9 @@ class MetadataContext {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
final Attribute attribute = attributeFactory.buildAttribute( jpa2Mapping, property );
|
final Attribute attribute = attributeFactory.buildAttribute( jpa2Mapping, property );
|
||||||
jpa2Mapping.getBuilder().addAttribute( attribute );
|
if ( attribute != null ) {
|
||||||
|
jpa2Mapping.getBuilder().addAttribute( attribute );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
jpa2Mapping.lock();
|
jpa2Mapping.lock();
|
||||||
populateStaticMetamodel( jpa2Mapping );
|
populateStaticMetamodel( jpa2Mapping );
|
||||||
|
@ -192,7 +194,9 @@ class MetadataContext {
|
||||||
while ( properties.hasNext() ) {
|
while ( properties.hasNext() ) {
|
||||||
final Property property = properties.next();
|
final Property property = properties.next();
|
||||||
final Attribute attribute = attributeFactory.buildAttribute( jpa2Mapping, property );
|
final Attribute attribute = attributeFactory.buildAttribute( jpa2Mapping, property );
|
||||||
jpa2Mapping.getBuilder().addAttribute( attribute );
|
if ( attribute != null ) {
|
||||||
|
jpa2Mapping.getBuilder().addAttribute( attribute );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
jpa2Mapping.lock();
|
jpa2Mapping.lock();
|
||||||
populateStaticMetamodel( jpa2Mapping );
|
populateStaticMetamodel( jpa2Mapping );
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -274,6 +274,18 @@ public class MetadataTest extends TestCase {
|
||||||
assertNull( thing.getSupertype() );
|
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) {
|
private void ensureProperMember(Set<?> attributes) {
|
||||||
//we do not update the set so we are safe
|
//we do not update the set so we are safe
|
||||||
@SuppressWarnings( "unchecked" )
|
@SuppressWarnings( "unchecked" )
|
||||||
|
@ -321,7 +333,9 @@ public class MetadataTest extends TestCase {
|
||||||
Feline.class,
|
Feline.class,
|
||||||
Garden.class,
|
Garden.class,
|
||||||
Flower.class,
|
Flower.class,
|
||||||
JoinedManyToOneOwner.class
|
JoinedManyToOneOwner.class,
|
||||||
|
Parent.class,
|
||||||
|
Child.class
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue