Author: epbernard
Date: 2009-08-12 19:58:14 -0400 (Wed, 12 Aug 2009)
New Revision: 17276
Added:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Address.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Room.java
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/ManagedTypeImpl.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Fridge.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/House.java
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java
Log:
EJB-456 add more tests on collection of elements Set List Map
Modified:
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/ManagedTypeImpl.java
===================================================================
---
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/ManagedTypeImpl.java 2009-08-12
22:51:01 UTC (rev 17275)
+++
core/trunk/entitymanager/src/main/java/org/hibernate/ejb/metamodel/ManagedTypeImpl.java 2009-08-12
23:58:14 UTC (rev 17276)
@@ -171,7 +171,7 @@
public <K, V> MapAttribute<X, K, V> getDeclaredMap(String name,
Class<K> keyType, Class<V> valueType) {
final PluralAttribute<X,?,?> 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<X, K, V> result = ( MapAttribute<X, K, V> ) attr;
if ( result.getKeyJavaType() != keyType ) {
Added:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Address.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Address.java
(rev 0)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Address.java 2009-08-12
23:58:14 UTC (rev 17276)
@@ -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;
+ }
+}
Modified:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Fridge.java
===================================================================
---
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Fridge.java 2009-08-12
22:51:01 UTC (rev 17275)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Fridge.java 2009-08-12
23:58:14 UTC (rev 17276)
@@ -3,6 +3,7 @@
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
+import javax.persistence.Basic;
/**
* @author Emmanuel Bernard
@@ -24,6 +25,7 @@
this.id = id;
}
+ @Basic(optional = false)
public String getBrand() {
return brand;
}
Modified:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/House.java
===================================================================
---
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/House.java 2009-08-12
22:51:01 UTC (rev 17275)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/House.java 2009-08-12
23:58:14 UTC (rev 17276)
@@ -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,8 +17,40 @@
@Entity
public class House {
private Key key;
- private String address1;
+ private Address address;
+ private Set<Room> rooms;
+ private Map<String, Room> roomsByName;
+ private List<Room> roomsBySize;
+ @ElementCollection
+ @OrderColumn(name = "size_order")
+ public List<Room> getRoomsBySize() {
+ return roomsBySize;
+ }
+
+ public void setRoomsBySize(List<Room> roomsBySize) {
+ this.roomsBySize = roomsBySize;
+ }
+
+ @ElementCollection
+ @MapKeyColumn(name="room_name")
+ public Map<String, Room> getRoomsByName() {
+ return roomsByName;
+ }
+
+ public void setRoomsByName(Map<String, Room> roomsByName) {
+ this.roomsByName = roomsByName;
+ }
+
+ @ElementCollection
+ public Set<Room> getRooms() {
+ return rooms;
+ }
+
+ public void setRooms(Set<Room> rooms) {
+ this.rooms = rooms;
+ }
+
@EmbeddedId
public Key getKey() {
return key;
@@ -21,12 +60,12 @@
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 {
Modified:
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java
===================================================================
---
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java 2009-08-12
22:51:01 UTC (rev 17275)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/MetadataTest.java 2009-08-12
23:58:14 UTC (rev 17276)
@@ -7,6 +7,11 @@
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 @@
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<? super Fridge, ?> attribute = entityType.getDeclaredAttribute(
"temperature" );
assertNotNull( attribute );
assertEquals( "temperature", attribute.getName() );
@@ -90,10 +97,57 @@
}
}
assertTrue( found );
+ }
+ public void testEmbeddable() throws Exception {
+ final EntityType<House> entityType = factory.getMetamodel().entity( House.class
);
+ final SingularAttribute<? super House,Address> address =
entityType.getDeclaredSingularAttribute(
+ "address",
+ Address.class
+ );
+ assertNotNull( address );
+ assertEquals( Attribute.PersistentAttributeType.EMBEDDED,
address.getPersistentAttributeType() );
+ assertFalse( address.isCollection() );
+ assertFalse( address.isAssociation() );
+ final EmbeddableType<Address> addressType = (EmbeddableType<Address>)
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<Address> directType = factory.getMetamodel().embeddable(
Address.class );
+ assertNotNull( directType );
+ assertEquals( Type.PersistenceType.EMBEDDABLE, directType.getPersistenceType() );
}
- //TODO test embedded
+
+ public void testElementCollection() throws Exception {
+ final EntityType<House> entityType = factory.getMetamodel().entity( House.class
);
+ final SetAttribute<House,Room> 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<House,String,Room> 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<House,Room> 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
Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Room.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Room.java
(rev 0)
+++
core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/metadata/Room.java 2009-08-12
23:58:14 UTC (rev 17276)
@@ -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;
+ }
+}