EJB-456 add more tests on collection of elements Set List Map

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@17276 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2009-08-12 23:58:14 +00:00
parent 93cef4f9f1
commit 506614cc1f
6 changed files with 175 additions and 9 deletions

View File

@ -171,7 +171,7 @@ public abstract class ManagedTypeImpl<X> implements ManagedType<X>, Serializable
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 ) {

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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<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() {
@ -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 {

View File

@ -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<? super Fridge, ?> 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<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() );
}
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

View File

@ -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;
}
}