|
Hibernate example source code file (MetadataTest.java)
The Hibernate MetadataTest.java source code/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2009 by Red Hat Inc and/or its affiliates or by * third-party contributors as indicated by either @author tags or express * copyright attribution statements applied by the authors. All * third-party contributions are distributed under license by Red Hat Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package org.hibernate.ejb.test.metadata; import javax.persistence.EntityManagerFactory; import javax.persistence.metamodel.Attribute; import javax.persistence.metamodel.Bindable; import javax.persistence.metamodel.EmbeddableType; import javax.persistence.metamodel.EntityType; import javax.persistence.metamodel.IdentifiableType; import javax.persistence.metamodel.ListAttribute; import javax.persistence.metamodel.MapAttribute; import javax.persistence.metamodel.MappedSuperclassType; import javax.persistence.metamodel.PluralAttribute; import javax.persistence.metamodel.SetAttribute; import javax.persistence.metamodel.SingularAttribute; import javax.persistence.metamodel.Type; import java.util.Set; import org.hibernate.cfg.Configuration; import org.hibernate.ejb.metamodel.MetamodelImpl; import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.engine.spi.SessionFactoryImplementor; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; /** * @author Emmanuel Bernard */ public class MetadataTest extends BaseEntityManagerFunctionalTestCase { @Test public void testBaseOfService() throws Exception { EntityManagerFactory emf = entityManagerFactory(); assertNotNull( emf.getMetamodel() ); final EntityType<Fridge> entityType = emf.getMetamodel().entity( Fridge.class ); assertNotNull( entityType ); } @Test @SuppressWarnings({ "unchecked" }) public void testBuildingMetamodelWithParameterizedCollection() { Configuration cfg = new Configuration( ); // configure( cfg ); cfg.addAnnotatedClass( WithGenericCollection.class ); cfg.buildMappings(); SessionFactoryImplementor sfi = (SessionFactoryImplementor) cfg.buildSessionFactory( serviceRegistry() ); MetamodelImpl.buildMetamodel( cfg.getClassMappings(), sfi ); } @Test public void testLogicalManyToOne() throws Exception { final EntityType<JoinedManyToOneOwner> entityType = entityManagerFactory().getMetamodel().entity( JoinedManyToOneOwner.class ); final SingularAttribute attr = entityType.getDeclaredSingularAttribute( "house" ); assertEquals( Attribute.PersistentAttributeType.MANY_TO_ONE, attr.getPersistentAttributeType() ); assertEquals( House.class, attr.getBindableJavaType() ); final EntityType<House> houseType = entityManagerFactory().getMetamodel().entity( House.class ); assertEquals( houseType.getBindableJavaType(), attr.getBindableJavaType() ); } @Test public void testEntity() throws Exception { final EntityType<Fridge> fridgeType = entityManagerFactory().getMetamodel().entity( Fridge.class ); assertEquals( Fridge.class, fridgeType.getBindableJavaType() ); assertEquals( Bindable.BindableType.ENTITY_TYPE, fridgeType.getBindableType() ); SingularAttribute<Fridge,Integer> wrapped = fridgeType.getDeclaredSingularAttribute( "temperature", Integer.class ); assertNotNull( wrapped ); SingularAttribute<Fridge,Integer> primitive = fridgeType.getDeclaredSingularAttribute( "temperature", int.class ); assertNotNull( primitive ); assertNotNull( fridgeType.getDeclaredSingularAttribute( "temperature" ) ); assertNotNull( fridgeType.getDeclaredAttribute( "temperature" ) ); final SingularAttribute<Fridge, Long> id = fridgeType.getDeclaredId( Long.class ); assertNotNull( id ); assertTrue( id.isId() ); try { fridgeType.getDeclaredId( java.util.Date.class ); fail( "expecting failure" ); } catch ( IllegalArgumentException ignore ) { // expected result } final SingularAttribute<? super Fridge, Long> id2 = fridgeType.getId( Long.class ); assertNotNull( id2 ); assertEquals( "Fridge", fridgeType.getName() ); assertEquals( Long.class, fridgeType.getIdType().getJavaType() ); assertTrue( fridgeType.hasSingleIdAttribute() ); assertFalse( fridgeType.hasVersionAttribute() ); assertEquals( Type.PersistenceType.ENTITY, fridgeType.getPersistenceType() ); assertEquals( 3, fridgeType.getDeclaredAttributes().size() ); final EntityType<House> houseType = entityManagerFactory().getMetamodel().entity( House.class ); assertEquals( "org.hibernate.ejb.test.metadata.House", houseType.getName() ); assertTrue( houseType.hasSingleIdAttribute() ); final SingularAttribute<House, House.Key> houseId = houseType.getDeclaredId( House.Key.class ); assertNotNull( houseId ); assertTrue( houseId.isId() ); assertEquals( Attribute.PersistentAttributeType.EMBEDDED, houseId.getPersistentAttributeType() ); final EntityType<Person> personType = entityManagerFactory().getMetamodel().entity( Person.class ); assertEquals( "Homo", personType.getName() ); assertFalse( personType.hasSingleIdAttribute() ); final Set<SingularAttribute ids = personType.getIdClassAttributes(); assertNotNull( ids ); assertEquals( 2, ids.size() ); for (SingularAttribute<? super Person,?> localId : ids) { assertTrue( localId.isId() ); } final EntityType<FoodItem> foodType = entityManagerFactory().getMetamodel().entity( FoodItem.class ); assertTrue( foodType.hasVersionAttribute() ); final SingularAttribute<? super FoodItem, Long> version = foodType.getVersion( Long.class ); assertNotNull( version ); assertTrue( version.isVersion() ); assertEquals( 3, foodType.getDeclaredAttributes().size() ); } @Test public void testBasic() throws Exception { final EntityType<Fridge> entityType = entityManagerFactory().getMetamodel().entity( Fridge.class ); final SingularAttribute<? super Fridge,Integer> singularAttribute = entityType.getDeclaredSingularAttribute( "temperature", Integer.class ); // assertEquals( Integer.class, singularAttribute.getBindableJavaType() ); // assertEquals( Integer.class, singularAttribute.getType().getJavaType() ); assertEquals( int.class, singularAttribute.getBindableJavaType() ); assertEquals( int.class, singularAttribute.getType().getJavaType() ); assertEquals( Bindable.BindableType.SINGULAR_ATTRIBUTE, singularAttribute.getBindableType() ); assertFalse( singularAttribute.isId() ); assertFalse( singularAttribute.isOptional() ); assertFalse( entityType.getDeclaredSingularAttribute( "brand", String.class ).isOptional() ); assertEquals( Type.PersistenceType.BASIC, singularAttribute.getType().getPersistenceType() ); final Attribute<? super Fridge, ?> attribute = entityType.getDeclaredAttribute( "temperature" ); assertNotNull( attribute ); assertEquals( "temperature", attribute.getName() ); assertEquals( Fridge.class, attribute.getDeclaringType().getJavaType() ); assertEquals( Attribute.PersistentAttributeType.BASIC, attribute.getPersistentAttributeType() ); // assertEquals( Integer.class, attribute.getJavaType() ); assertEquals( int.class, attribute.getJavaType() ); assertFalse( attribute.isAssociation() ); assertFalse( attribute.isCollection() ); boolean found = false; for (Attribute<Fridge, ?> attr : entityType.getDeclaredAttributes() ) { if ("temperature".equals( attr.getName() ) ) { found = true; break; } } assertTrue( found ); } @Test public void testEmbeddable() throws Exception { final EntityType<House> entityType = entityManagerFactory().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.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 = entityManagerFactory().getMetamodel().embeddable( Address.class ); assertNotNull( directType ); assertEquals( Type.PersistenceType.EMBEDDABLE, directType.getPersistenceType() ); } @Test public void testCollection() throws Exception { final EntityType<Garden> entiytype = entityManagerFactory().getMetamodel().entity( Garden.class ); final Set<PluralAttribute attributes = entiytype.getPluralAttributes(); assertEquals( 1, attributes.size() ); PluralAttribute<? super Garden, ?, ?> flowers = attributes.iterator().next(); assertTrue( flowers instanceof ListAttribute ); } @Test public void testElementCollection() throws Exception { final EntityType<House> entityType = entityManagerFactory().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.getDeclaredPluralAttributes().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() ); } @Test public void testHierarchy() { final EntityType<Cat> cat = entityManagerFactory().getMetamodel().entity( Cat.class ); assertNotNull( cat ); assertEquals( 7, cat.getAttributes().size() ); assertEquals( 1, cat.getDeclaredAttributes().size() ); ensureProperMember(cat.getDeclaredAttributes()); assertTrue( cat.hasVersionAttribute() ); assertEquals( "version", cat.getVersion(Long.class).getName() ); verifyDeclaredVersionNotPresent( cat ); verifyDeclaredIdNotPresentAndIdPresent(cat); assertEquals( Type.PersistenceType.MAPPED_SUPERCLASS, cat.getSupertype().getPersistenceType() ); MappedSuperclassType<Cattish> cattish = (MappedSuperclassType Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate MetadataTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.