diff --git a/entitymanager/src/main/java/org/hibernate/ejb/metamodel/ManagedTypeImpl.java b/entitymanager/src/main/java/org/hibernate/ejb/metamodel/ManagedTypeImpl.java index 170564beea..e906ab5c87 100644 --- a/entitymanager/src/main/java/org/hibernate/ejb/metamodel/ManagedTypeImpl.java +++ b/entitymanager/src/main/java/org/hibernate/ejb/metamodel/ManagedTypeImpl.java @@ -171,7 +171,7 @@ public abstract class ManagedTypeImpl implements ManagedType, Serializable public MapAttribute getDeclaredMap(String name, Class keyType, Class valueType) { final PluralAttribute attr = declaredCollections.get( name ); final String error = "MapAttribute "; - checkTypeForPluralAttributes( error, attr, name, valueType, PluralAttribute.CollectionType.LIST ); + checkTypeForPluralAttributes( error, attr, name, valueType, PluralAttribute.CollectionType.MAP ); @SuppressWarnings( "unchecked") final MapAttribute result = ( MapAttribute ) attr; if ( result.getKeyJavaType() != keyType ) { diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Address.java b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Address.java new file mode 100644 index 0000000000..28a24ec57f --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Address.java @@ -0,0 +1,42 @@ +package org.hibernate.ejb.test.metadata; + +import javax.persistence.Embeddable; +import javax.persistence.Basic; + +/** + * @author Emmanuel Bernard + */ +@Embeddable +public class Address { + + private String address1; + + private String address2; + private String city; + + @Basic(optional = true) + public String getAddress1() { + return address1; + } + + public void setAddress1(String address1) { + this.address1 = address1; + } + + @Basic(optional = false) + public String getAddress2() { + return address2; + } + + public void setAddress2(String address2) { + this.address2 = address2; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } +} diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Fridge.java b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Fridge.java index e79cf76067..7411dd14c6 100644 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Fridge.java +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Fridge.java @@ -3,6 +3,7 @@ package org.hibernate.ejb.test.metadata; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.GeneratedValue; +import javax.persistence.Basic; /** * @author Emmanuel Bernard @@ -24,6 +25,7 @@ public class Fridge { this.id = id; } + @Basic(optional = false) public String getBrand() { return brand; } diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/House.java b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/House.java index 7bee9d6bd8..041147da13 100644 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/House.java +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/House.java @@ -1,8 +1,15 @@ package org.hibernate.ejb.test.metadata; import java.io.Serializable; +import java.util.Set; +import java.util.Map; +import java.util.List; import javax.persistence.EmbeddedId; import javax.persistence.Entity; +import javax.persistence.ElementCollection; +import javax.persistence.CollectionTable; +import javax.persistence.MapKeyColumn; +import javax.persistence.OrderColumn; /** * @author Emmanuel Bernard @@ -10,7 +17,39 @@ import javax.persistence.Entity; @Entity public class House { private Key key; - private String address1; + private Address address; + private Set rooms; + private Map roomsByName; + private List roomsBySize; + + @ElementCollection + @OrderColumn(name = "size_order") + public List getRoomsBySize() { + return roomsBySize; + } + + public void setRoomsBySize(List roomsBySize) { + this.roomsBySize = roomsBySize; + } + + @ElementCollection + @MapKeyColumn(name="room_name") + public Map getRoomsByName() { + return roomsByName; + } + + public void setRoomsByName(Map roomsByName) { + this.roomsByName = roomsByName; + } + + @ElementCollection + public Set getRooms() { + return rooms; + } + + public void setRooms(Set rooms) { + this.rooms = rooms; + } @EmbeddedId public Key getKey() { @@ -21,12 +60,12 @@ public class House { this.key = key; } - public String getAddress1() { - return address1; + public Address getAddress() { + return address; } - public void setAddress1(String address1) { - this.address1 = address1; + public void setAddress(Address address) { + this.address = address; } public static class Key implements Serializable { diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java index 9f34bedd83..1a5ef21c60 100644 --- a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java @@ -7,6 +7,11 @@ import javax.persistence.metamodel.Bindable; import javax.persistence.metamodel.SingularAttribute; import javax.persistence.metamodel.Type; import javax.persistence.metamodel.Attribute; +import javax.persistence.metamodel.EmbeddableType; +import javax.persistence.metamodel.SetAttribute; +import javax.persistence.metamodel.PluralAttribute; +import javax.persistence.metamodel.MapAttribute; +import javax.persistence.metamodel.ListAttribute; import org.hibernate.ejb.test.TestCase; @@ -72,7 +77,9 @@ public class MetadataTest extends TestCase { assertEquals( Bindable.BindableType.SINGULAR_ATTRIBUTE, singularAttribute.getBindableType() ); assertFalse( singularAttribute.isId() ); assertFalse( singularAttribute.isOptional() ); + assertFalse( entityType.getDeclaredSingularAttribute( "brand", String.class ).isOptional() ); assertEquals( Integer.class, singularAttribute.getType().getJavaType() ); + assertEquals( Type.PersistenceType.BASIC, singularAttribute.getType().getPersistenceType() ); final Attribute attribute = entityType.getDeclaredAttribute( "temperature" ); assertNotNull( attribute ); assertEquals( "temperature", attribute.getName() ); @@ -90,10 +97,57 @@ public class MetadataTest extends TestCase { } } assertTrue( found ); - - } - //TODO test embedded + + public void testEmbeddable() throws Exception { + final EntityType entityType = factory.getMetamodel().entity( House.class ); + final SingularAttribute address = entityType.getDeclaredSingularAttribute( + "address", + Address.class + ); + assertNotNull( address ); + assertEquals( Attribute.PersistentAttributeType.EMBEDDED, address.getPersistentAttributeType() ); + assertFalse( address.isCollection() ); + assertFalse( address.isAssociation() ); + final EmbeddableType
addressType = (EmbeddableType
) address.getType(); + assertEquals( Type.PersistenceType.EMBEDDABLE, addressType.getPersistenceType() ); + assertEquals( 3, addressType.getDeclaredAttributes().size() ); + assertTrue( addressType.getDeclaredSingularAttribute( "address1" ).isOptional() ); + assertFalse( addressType.getDeclaredSingularAttribute( "address2" ).isOptional() ); + + final EmbeddableType
directType = factory.getMetamodel().embeddable( Address.class ); + assertNotNull( directType ); + assertEquals( Type.PersistenceType.EMBEDDABLE, directType.getPersistenceType() ); + } + + public void testElementCollection() throws Exception { + final EntityType entityType = factory.getMetamodel().entity( House.class ); + final SetAttribute rooms = entityType.getDeclaredSet( "rooms", Room.class ); + assertNotNull( rooms ); + assertTrue( rooms.isAssociation() ); + assertTrue( rooms.isCollection() ); + assertEquals( Attribute.PersistentAttributeType.ELEMENT_COLLECTION, rooms.getPersistentAttributeType() ); + assertEquals( Room.class, rooms.getBindableJavaType() ); + assertEquals( Bindable.BindableType.PLURAL_ATTRIBUTE, rooms.getBindableType() ); + assertEquals( Set.class, rooms.getJavaType() ); + assertEquals( PluralAttribute.CollectionType.SET, rooms.getCollectionType() ); + assertEquals( 3, entityType.getDeclaredCollections().size() ); + assertEquals( Type.PersistenceType.EMBEDDABLE, rooms.getElementType().getPersistenceType() ); + + final MapAttribute roomsByName = entityType.getDeclaredMap( + "roomsByName", String.class, Room.class + ); + assertNotNull( roomsByName ); + assertEquals( String.class, roomsByName.getKeyJavaType() ); + assertEquals( Type.PersistenceType.BASIC, roomsByName.getKeyType().getPersistenceType() ); + assertEquals( PluralAttribute.CollectionType.MAP, roomsByName.getCollectionType() ); + + final ListAttribute roomsBySize = entityType.getDeclaredList( "roomsBySize", Room.class ); + assertNotNull( roomsBySize ); + assertEquals( Type.PersistenceType.EMBEDDABLE, roomsBySize.getElementType().getPersistenceType() ); + assertEquals( PluralAttribute.CollectionType.LIST, roomsBySize.getCollectionType() ); + } + //todo test plural @Override diff --git a/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Room.java b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Room.java new file mode 100644 index 0000000000..24e5aec621 --- /dev/null +++ b/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Room.java @@ -0,0 +1,29 @@ +package org.hibernate.ejb.test.metadata; + +import java.math.BigDecimal; +import javax.persistence.Embeddable; + +/** + * @author Emmanuel Bernard + */ +@Embeddable +public class Room { + private BigDecimal length; + private BigDecimal width; + + public BigDecimal getLength() { + return length; + } + + public void setLength(BigDecimal length) { + this.length = length; + } + + public BigDecimal getWidth() { + return width; + } + + public void setWidth(BigDecimal width) { + this.width = width; + } +}