HHH-3906 EJB-436 ANN-830 Move to JPA 2 API as a dependency

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@16523 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2009-05-07 21:34:21 +00:00
parent 2e377bbc08
commit 11668ca67c
29 changed files with 465 additions and 96 deletions

View File

@ -58,8 +58,8 @@
<artifactId>hibernate-commons-annotations</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<groupId>org.hibernate.java-persistence</groupId>
<artifactId>jpa-api</artifactId>
</dependency>
<dependency>
<groupId>javassist</groupId>
@ -96,9 +96,9 @@
<version>${version}</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<groupId>org.hibernate.java-persistence</groupId>
<artifactId>jpa-api</artifactId>
<version>2.0.Beta1</version>
</dependency>
<dependency>
<groupId>javassist</groupId>

View File

@ -70,6 +70,7 @@ import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Transient;
import javax.persistence.Version;
import javax.persistence.ElementCollection;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
@ -1238,7 +1239,9 @@ public final class AnnotationBinder {
}
else if ( joinColumns == null &&
( property.isAnnotationPresent( OneToMany.class )
|| property.isAnnotationPresent( CollectionOfElements.class ) ) ) {
|| property.isAnnotationPresent( CollectionOfElements.class ) //legacy Hibernate
|| property.isAnnotationPresent( ElementCollection.class )
) ) {
OneToMany oneToMany = property.getAnnotation( OneToMany.class );
String mappedBy = oneToMany != null ?
oneToMany.mappedBy() :
@ -1454,11 +1457,13 @@ public final class AnnotationBinder {
}
else if ( property.isAnnotationPresent( OneToMany.class )
|| property.isAnnotationPresent( ManyToMany.class )
|| property.isAnnotationPresent( CollectionOfElements.class )
|| property.isAnnotationPresent( CollectionOfElements.class ) //legacy Hibernate
|| property.isAnnotationPresent( ElementCollection.class )
|| property.isAnnotationPresent( ManyToAny.class ) ) {
OneToMany oneToManyAnn = property.getAnnotation( OneToMany.class );
ManyToMany manyToManyAnn = property.getAnnotation( ManyToMany.class );
CollectionOfElements collectionOfElementsAnn = property.getAnnotation( CollectionOfElements.class );
ElementCollection elementCollectionAnn = property.getAnnotation( ElementCollection.class );
CollectionOfElements collectionOfElementsAnn = property.getAnnotation( CollectionOfElements.class ); //legacy hibernate
org.hibernate.annotations.IndexColumn indexAnn = property.getAnnotation(
org.hibernate.annotations.IndexColumn.class
);
@ -1585,7 +1590,9 @@ public final class AnnotationBinder {
collectionBinder.setCascadeStrategy( getCascadeStrategy( oneToManyAnn.cascade(), hibernateCascade ) );
collectionBinder.setOneToMany( true );
}
else if ( collectionOfElementsAnn != null ) {
else if ( elementCollectionAnn != null
|| collectionOfElementsAnn != null //Hibernate legacy
) {
for (Ejb3JoinColumn column : joinColumns) {
if ( column.isSecondary() ) {
throw new NotYetImplementedException( "Collections having FK in secondary table" );
@ -1593,8 +1600,11 @@ public final class AnnotationBinder {
}
collectionBinder.setFkJoinColumns( joinColumns );
mappedBy = "";
final Class<?> targetElement = elementCollectionAnn != null ?
elementCollectionAnn.targetClass() :
collectionOfElementsAnn.targetElement();
collectionBinder.setTargetEntity(
mappings.getReflectionManager().toXClass( collectionOfElementsAnn.targetElement() )
mappings.getReflectionManager().toXClass( targetElement )
);
//collectionBinder.setCascadeStrategy( getCascadeStrategy( embeddedCollectionAnn.cascade(), hibernateCascade ) );
collectionBinder.setOneToMany( true );

View File

@ -38,6 +38,7 @@ import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
import javax.persistence.ElementCollection;
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
@ -449,7 +450,9 @@ public abstract class CollectionBinder {
tableBinder, mappings
);
if ( collectionType.isAnnotationPresent( Embeddable.class )
|| property.isAnnotationPresent( CollectionOfElements.class ) ) {
|| property.isAnnotationPresent( CollectionOfElements.class ) //legacy hibernate
|| property.isAnnotationPresent( ElementCollection.class ) //JPA 2
) {
// do it right away, otherwise @ManyToon on composite element call addSecondPass
// and raise a ConcurrentModificationException
//sp.doSecondPass( CollectionHelper.EMPTY_MAP );
@ -483,7 +486,8 @@ public abstract class CollectionBinder {
Fetch fetch = property.getAnnotation( Fetch.class );
OneToMany oneToMany = property.getAnnotation( OneToMany.class );
ManyToMany manyToMany = property.getAnnotation( ManyToMany.class );
CollectionOfElements elements = property.getAnnotation( CollectionOfElements.class );
CollectionOfElements collectionOfElements = property.getAnnotation( CollectionOfElements.class ); //legacy hibernate
ElementCollection elementCollection = property.getAnnotation( ElementCollection.class ); //jpa 2
ManyToAny manyToAny = property.getAnnotation( ManyToAny.class );
FetchType fetchType;
if ( oneToMany != null ) {
@ -492,8 +496,11 @@ public abstract class CollectionBinder {
else if ( manyToMany != null ) {
fetchType = manyToMany.fetch();
}
else if ( elements != null ) {
fetchType = elements.fetch();
else if ( elementCollection != null ) {
fetchType = elementCollection.fetch();
}
else if ( collectionOfElements != null ) {
fetchType = collectionOfElements.fetch();
}
else if ( manyToAny != null ) {
fetchType = FetchType.LAZY;

View File

@ -102,6 +102,7 @@ import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;
import javax.persistence.ElementCollection;
import org.dom4j.Attribute;
import org.dom4j.Element;
@ -563,7 +564,8 @@ public class JPAOverridenAnnotationReader implements AnnotationReader {
defaultToJoinTable = defaultToJoinTable &&
( ( annotationClass == ManyToMany.class && StringHelper.isEmpty( ( (ManyToMany) annotation ).mappedBy() ) )
|| ( annotationClass == OneToMany.class && StringHelper.isEmpty( ( (OneToMany) annotation ).mappedBy() ) )
|| ( annotationClass == CollectionOfElements.class )
|| ( annotationClass == CollectionOfElements.class ) //legacy Hibernate
|| ( annotationClass == ElementCollection.class )
);
final Class<JoinTable> annotationType = JoinTable.class;
if ( defaultToJoinTable
@ -699,7 +701,41 @@ public class JPAOverridenAnnotationReader implements AnnotationReader {
annotation = getJavaAnnotation( Columns.class );
addIfNotNull( annotationList, annotation );
}
else if ( isJavaAnnotationPresent( CollectionOfElements.class ) ) {
else if ( isJavaAnnotationPresent( ElementCollection.class ) ) { //JPA2
annotation = overridesDefaultsInJoinTable( getJavaAnnotation( ElementCollection.class ), defaults );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( JoinColumn.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( JoinColumns.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( PrimaryKeyJoinColumn.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( PrimaryKeyJoinColumns.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( MapKey.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( OrderBy.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( AttributeOverride.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( AttributeOverrides.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( AssociationOverride.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( AssociationOverrides.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( Lob.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( Enumerated.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( Temporal.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( Column.class );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( Columns.class );
addIfNotNull( annotationList, annotation );
}
else if ( isJavaAnnotationPresent( CollectionOfElements.class ) ) { //legacy Hibernate
annotation = overridesDefaultsInJoinTable( getJavaAnnotation( CollectionOfElements.class ), defaults );
addIfNotNull( annotationList, annotation );
annotation = getJavaAnnotation( JoinColumn.class );

View File

@ -7,6 +7,7 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.ElementCollection;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.IndexColumn;
@ -40,7 +41,7 @@ public class Contest {
this.results = results;
}
@CollectionOfElements
@ElementCollection
@IndexColumn(name = "pos", base=1)
public Month[] getHeldIn() {
return heldIn;

View File

@ -8,6 +8,7 @@ import javax.persistence.ManyToMany;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.CascadeType;
import javax.persistence.ElementCollection;
import javax.validation.constraints.NotNull;
import javax.validation.Valid;
@ -54,7 +55,8 @@ public class Screen {
this.powerSupply = powerSupply;
}
@CollectionOfElements @Valid
@ElementCollection
@Valid
public Set<DisplayConnector> getConnectors() {
return connectors;
}

View File

@ -16,6 +16,7 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ElementCollection;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.IndexColumn;
@ -66,7 +67,7 @@ public class Boy {
this.lastName = lastName;
}
@CollectionOfElements
@CollectionOfElements //keep hibernate legacy for test purposes
public Set<String> getNickNames() {
return nickNames;
}
@ -75,7 +76,7 @@ public class Boy {
this.nickNames = nickName;
}
@CollectionOfElements
@ElementCollection
@JoinTable(name = "ScorePerNickName", joinColumns = @JoinColumn(name = "BoyId"))
@Column(name = "score", nullable = false)
public Map<String, Integer> getScorePerNickName() {
@ -86,7 +87,7 @@ public class Boy {
this.scorePerNickName = scorePerNickName;
}
@CollectionOfElements
@ElementCollection
@JoinTable(
name = "BoyFavoriteNumbers",
joinColumns = @JoinColumn(name = "BoyId")
@ -101,7 +102,7 @@ public class Boy {
this.favoriteNumbers = favoriteNumbers;
}
@CollectionOfElements
@CollectionOfElements //TODO migration to ElementCollection "element.serial"??
@AttributeOverride(name = "element.serial", column = @Column(name = "serial_nbr"))
public Set<Toy> getFavoriteToys() {
return favoriteToys;
@ -111,7 +112,7 @@ public class Boy {
this.favoriteToys = favoriteToys;
}
@CollectionOfElements
@ElementCollection
@Enumerated(EnumType.STRING)
public Set<Character> getCharacters() {
return characters;
@ -121,7 +122,7 @@ public class Boy {
this.characters = characters;
}
@CollectionOfElements(fetch = FetchType.EAGER)
@ElementCollection(fetch = FetchType.EAGER)
//@Where(clause = "b_likes=false")
public Set<CountryAttitude> getCountryAttitudes() {
return countryAttitudes;

View File

@ -1,18 +1,18 @@
//$Id$
package org.hibernate.test.annotations.collectionelement;
import java.util.HashMap;
import java.util.Map;
import java.util.Locale;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Embeddable;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.MapKey;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.MapKey;
/**
* @author Emmanuel Bernard
@ -32,7 +32,7 @@ public class LocalizedString implements Serializable {
private Map<String, String> variations =
new HashMap<String, String>( 1 );
@CollectionOfElements
@ElementCollection
@MapKey( columns = @Column( name = "language_code" ) )
@Fetch( FetchMode.JOIN )
@Filter( name = "selectedLocale",

View File

@ -4,12 +4,11 @@ package org.hibernate.test.annotations.collectionelement;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.MapKey;
import org.hibernate.annotations.Sort;
import org.hibernate.annotations.SortType;
@ -25,7 +24,7 @@ public class Matrix {
private Integer id;
@MapKey(type = @Type(type="integer") )
@CollectionOfElements
@ElementCollection
@Sort(type = SortType.NATURAL)
@Type(type = "float")
private SortedMap<Integer, Float> values = new TreeMap<Integer, Float>();

View File

@ -6,6 +6,7 @@ package org.hibernate.test.annotations.collectionelement.deepcollectionelements;
*/
import java.util.List;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@ -13,7 +14,6 @@ import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.IndexColumn;
@Entity
@ -23,7 +23,7 @@ public class A {
@GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "aSequence" )
@SequenceGenerator( name = "aSequence", sequenceName = "seq_A" )
private int id;
@CollectionOfElements
@ElementCollection
@IndexColumn( name = "ndx" )
private List<B> listOfB;

View File

@ -3,10 +3,8 @@ package org.hibernate.test.annotations.collectionelement.deepcollectionelements;
import java.util.List;
import javax.persistence.Embeddable;
import javax.persistence.Transient;
import javax.persistence.OneToMany;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.IndexColumn;
@Embeddable

View File

@ -23,7 +23,7 @@ import org.hibernate.annotations.GenericGenerator;
public class Sale {
@Id @GeneratedValue private Integer id;
@CollectionOfElements
@CollectionOfElements //TODO migrate to @ElementCollection, what about @CollectionId
@JoinTable(
name = "contact",
joinColumns = @JoinColumn(name = "n_key_person"))

View File

@ -6,9 +6,10 @@ package org.hibernate.test.annotations.generics;
* @author Edward Costello
* @author Paolo Perrotta
*/
import java.util.HashSet;
import java.util.Set;
import javax.persistence.ElementCollection;
import javax.persistence.Embeddable;
import javax.persistence.Embedded;
import javax.persistence.Entity;
@ -16,8 +17,6 @@ import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.annotations.CollectionOfElements;
public class Classes {
@Embeddable
@ -41,7 +40,7 @@ public class Classes {
@GeneratedValue(strategy=GenerationType.AUTO)
Long id;
@CollectionOfElements
@ElementCollection
Set<Edition<String>> editions = new HashSet<Edition<String>>();
}
}

View File

@ -34,7 +34,7 @@ public class Atmosphere {
@JoinTable(name = "Gas_per_key")
public Map<GasKey, Gas> gasesPerKey = new HashMap<GasKey, Gas>();
@CollectionOfElements
@CollectionOfElements //TODO migrate to @ElementCollection ; @MapKeyManyToMany ??
@Column(name="composition_rate")
@MapKeyManyToMany(joinColumns = @JoinColumn(name="gas_id"))
@JoinTable(name = "Composition", joinColumns = @JoinColumn(name = "atmosphere_id"))

View File

@ -1,15 +1,15 @@
//$Id$
package org.hibernate.test.annotations.target;
import java.util.Map;
import java.util.HashMap;
import java.util.Map;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import org.hibernate.annotations.MapKey;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.MapKeyManyToMany;
/**
@ -25,7 +25,7 @@ public class Brand {
@MapKey(targetElement = SizeImpl.class)
private Map<Size, Luggage> luggagesBySize = new HashMap<Size, Luggage>();
@CollectionOfElements(targetElement = SizeImpl.class)
@ElementCollection(targetClass = SizeImpl.class)
@MapKeyManyToMany(targetEntity = LuggageImpl.class)
private Map<Luggage, Size> sizePerLuggage = new HashMap<Luggage, Size>();

View File

@ -56,10 +56,9 @@
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
<scope>test</scope>
<groupId>org.hibernate.java-persistence</groupId>
<artifactId>jpa-api</artifactId>
<version>2.0.Beta1</version>
</dependency>
</dependencies>

View File

@ -1,4 +1,4 @@
// $Id:$
// $Id$
/*
* JBoss, the OpenSource EJB server Distributable under LGPL license. See terms of license at
* gnu.org.
@ -10,6 +10,7 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityNotFoundException;
import javax.persistence.EntityTransaction;
@ -22,6 +23,10 @@ import javax.persistence.PersistenceContextType;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import javax.persistence.TransactionRequiredException;
import javax.persistence.EntityManagerFactory;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.QueryBuilder;
import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.transaction.Status;
import javax.transaction.Synchronization;
@ -98,6 +103,10 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
}
}
public Query createQuery(CriteriaQuery criteriaQuery) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Query createNamedQuery(String name) {
//adjustFlushMode();
org.hibernate.Query namedQuery;
@ -204,6 +213,21 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
}
}
public <T> T find(Class<T> tClass, Object o, Map<String, Object> stringObjectMap) {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public <T> T find(Class<T> tClass, Object o, LockModeType lockModeType) {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public <T> T find(Class<T> tClass, Object o, LockModeType lockModeType, Map<String, Object> stringObjectMap) {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
private void checkTransactionNeeded() {
if ( persistenceContextType == PersistenceContextType.TRANSACTION && ! isTransactionInProgress() ) {
//no need to mark as rollback, no tx in progress
@ -277,6 +301,21 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
}
}
public void refresh(Object o, Map<String, Object> stringObjectMap) {
//FIXME
//To change body of implemented methods use File | Settings | File Templates.
}
public void refresh(Object o, LockModeType lockModeType) {
//FIXME
//To change body of implemented methods use File | Settings | File Templates.
}
public void refresh(Object o, LockModeType lockModeType, Map<String, Object> stringObjectMap) {
//FIXME
//To change body of implemented methods use File | Settings | File Templates.
}
public boolean contains(Object entity) {
try {
if ( entity != null
@ -295,6 +334,26 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
}
}
public LockModeType getLockMode(Object o) {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public void setProperty(String s, Object o) {
//FIXME
//To change body of implemented methods use File | Settings | File Templates.
}
public Map<String, Object> getProperties() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Set<String> getSupportedProperties() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public void flush() {
try {
if ( ! isTransactionInProgress() ) {
@ -326,6 +385,21 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
return tx;
}
public EntityManagerFactory getEntityManagerFactory() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public QueryBuilder getQueryBuilder() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Metamodel getMetamodel() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public void setFlushMode(FlushModeType flushModeType) {
this.flushModeType = flushModeType;
if ( flushModeType == FlushModeType.AUTO ) {
@ -349,6 +423,10 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
}
}
public void detach(Object entity) {
getSession().evict( entity );
}
public FlushModeType getFlushMode() {
FlushMode mode = getSession().getFlushMode();
if ( mode == FlushMode.AUTO ) {
@ -387,6 +465,11 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
}
}
public void lock(Object o, LockModeType lockModeType, Map<String, Object> stringObjectMap) {
//FIXME
//To change body of implemented methods use File | Settings | File Templates.
}
private LockMode getLockMode(LockModeType lockMode) {
switch ( lockMode ) {
case READ:
@ -431,6 +514,13 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
joinTransaction( false );
}
public <T> T unwrap(Class<T> clazz) {
if (clazz.equals( Session.class ) ) return (T) getSession();
if (clazz.equals( SessionImplementor.class ) ) return (T) getSession();
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
private void joinTransaction(boolean ignoreNotJoining) {
//set the joined status
getSession().isOpen(); //for sync

View File

@ -2,8 +2,12 @@
package org.hibernate.ejb;
import java.util.Map;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContextType;
import javax.persistence.Cache;
import javax.persistence.metamodel.Metamodel;
import javax.persistence.criteria.QueryBuilder;
import javax.persistence.spi.PersistenceUnitTransactionType;
import org.hibernate.SessionFactory;
@ -42,10 +46,35 @@ public class EntityManagerFactoryImpl implements HibernateEntityManagerFactory {
);
}
public QueryBuilder getQueryBuilder() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Metamodel getMetamodel() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public void close() {
sessionFactory.close();
}
public Map<String, Object> getProperties() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Set<String> getSupportedProperties() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Cache getCache() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public boolean isOpen() {
return ! sessionFactory.isClosed();
}

View File

@ -2,8 +2,23 @@
package org.hibernate.ejb;
import java.util.Map;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Modifier;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.persistence.spi.LoadState;
import org.hibernate.Hibernate;
import org.hibernate.intercept.FieldInterceptionHelper;
import org.hibernate.intercept.FieldInterceptor;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
/**
* Hibernate EJB3 persistence provider implementation
@ -132,6 +147,135 @@ public class HibernatePersistence implements javax.persistence.spi.PersistencePr
return configured != null ? configured.buildEntityManagerFactory() : null;
}
public LoadState isLoadedWithoutReference(Object proxy, String property) {
Object entity;
boolean sureFromUs = false;
if ( proxy instanceof HibernateProxy ) {
LazyInitializer li = ( ( HibernateProxy ) proxy ).getHibernateLazyInitializer();
if ( li.isUninitialized() ) {
return LoadState.NOT_LOADED;
}
else {
entity = li.getImplementation();
}
sureFromUs = true;
}
else {
entity = proxy;
}
//we are instrumenting but we can't assume we are the only ones
if ( FieldInterceptionHelper.isInstrumented( entity ) ) {
FieldInterceptor interceptor = FieldInterceptionHelper.extractFieldInterceptor( entity );
final boolean isInitialized = interceptor == null || interceptor.isInitialized( property );
LoadState state;
if (isInitialized && interceptor != null) {
state = LoadState.LOADED;
}
else if ( interceptor != null && (! isInitialized)) {
state = LoadState.NOT_LOADED;
}
else if ( sureFromUs ) { //interceptor == null
state = LoadState.LOADED;
}
else {
state = LoadState.UNKNOWN;
}
return state;
}
else {
//can't do sureFromUs ? LoadState.LOADED : LoadState.UNKNOWN;
//is that an association?
return LoadState.UNKNOWN;
}
}
public LoadState isLoadedWithReference(Object proxy, String property) {
//for sure we don't instrument and for sure it's not a lazy proxy
Object object = get(proxy, property);
return isLoaded( object );
}
private Object get(Object proxy, String property) {
final Class<?> clazz = proxy.getClass();
try {
try {
final Field field = clazz.getField( property );
setAccessibility( field );
return field.get( proxy );
}
catch ( NoSuchFieldException e ) {
final Method method = getMethod( clazz, property );
if (method != null) {
setAccessibility( method );
return method.invoke( proxy );
}
else {
throw new PersistenceException( "Unable to find field or method: "
+ clazz + "#"
+ property);
}
}
}
catch ( IllegalAccessException e ) {
throw new PersistenceException( "Unable to access field or method: "
+ clazz + "#"
+ property, e);
}
catch ( InvocationTargetException e ) {
throw new PersistenceException( "Unable to access field or method: "
+ clazz + "#"
+ property, e);
}
}
/**
* Returns the method with the specified name or <code>null</code> if it does not exist.
*
* @param clazz The class to check.
* @param methodName The method name.
*
* @return Returns the method with the specified name or <code>null</code> if it does not exist.
*/
public static Method getMethod(Class<?> clazz, String methodName) {
try {
char string[] = methodName.toCharArray();
string[0] = Character.toUpperCase( string[0] );
methodName = new String( string );
try {
return clazz.getMethod( "get" + methodName );
}
catch ( NoSuchMethodException e ) {
return clazz.getMethod( "is" + methodName );
}
}
catch ( NoSuchMethodException e ) {
return null;
}
}
public static void setAccessibility(Member member) {
if ( !Modifier.isPublic( member.getModifiers() ) ) {
//Sun's ease of use, sigh...
( ( AccessibleObject ) member ).setAccessible( true );
}
}
public LoadState isLoaded(Object o) {
if ( o instanceof HibernateProxy ) {
final boolean isInitialized = !( ( HibernateProxy ) o ).getHibernateLazyInitializer().isUninitialized();
return isInitialized ? LoadState.LOADED : LoadState.NOT_LOADED;
}
else if ( o instanceof PersistentCollection ) {
final boolean isInitialized = ( ( PersistentCollection ) o ).wasInitialized();
return isInitialized ? LoadState.LOADED : LoadState.NOT_LOADED;
}
else {
return LoadState.UNKNOWN;
}
}
/**
* create a factory from a canonical version
* @deprecated

View File

@ -7,6 +7,7 @@ import java.util.Date;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import java.util.Map;
import javax.persistence.FlushModeType;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
@ -14,6 +15,7 @@ import javax.persistence.Query;
import javax.persistence.TemporalType;
import static javax.persistence.TemporalType.*;
import javax.persistence.TransactionRequiredException;
import javax.persistence.LockModeType;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
@ -32,6 +34,7 @@ public class QueryImpl implements Query, HibernateQuery {
private HibernateEntityManagerImplementor em;
private Boolean isPositional = null;
private int maxResults = -1;
private int firstResult;
public QueryImpl(org.hibernate.Query query, AbstractEntityManagerImpl em) {
this.query = query;
@ -143,6 +146,10 @@ public class QueryImpl implements Query, HibernateQuery {
return this;
}
public int getMaxResults() {
return maxResults == -1 ? Integer.MAX_VALUE : maxResults; //stupid spec MAX_VALUE??
}
public Query setFirstResult(int firstResult) {
if ( firstResult < 0 ) {
throw new IllegalArgumentException(
@ -152,9 +159,14 @@ public class QueryImpl implements Query, HibernateQuery {
);
}
query.setFirstResult( firstResult );
this.firstResult = firstResult;
return this;
}
public int getFirstResult() {
return firstResult;
}
public Query setHint(String hintName, Object value) {
try {
if ( "org.hibernate.timeout".equals( hintName ) ) {
@ -192,6 +204,16 @@ public class QueryImpl implements Query, HibernateQuery {
return this;
}
public Map<String, Object> getHints() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Set<String> getSupportedHints() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Query setParameter(String name, Object value) {
try {
if ( value instanceof Collection ) {
@ -351,6 +373,16 @@ public class QueryImpl implements Query, HibernateQuery {
}
}
public Map<String, Object> getNamedParameters() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public List getPositionalParameters() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Query setFlushMode(FlushModeType flushMode) {
if ( flushMode == FlushModeType.AUTO ) {
query.setFlushMode( FlushMode.AUTO );
@ -360,4 +392,24 @@ public class QueryImpl implements Query, HibernateQuery {
}
return this;
}
public FlushModeType getFlushMode() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public Query setLockMode(LockModeType lockModeType) {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public LockModeType getLockMode() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public <T> T unwrap(Class<T> tClass) {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}

View File

@ -219,7 +219,7 @@ public class PackagedEntityManagerTest extends TestCase {
public void testListeners() throws Exception {
EntityManagerFactory emf = Persistence.createEntityManagerFactory( "manager1", new HashMap() );
EntityManager em = emf.createEntityManager();
EventListeners eventListeners = ( (SessionImplementor) em.getDelegate() ).getListeners();
EventListeners eventListeners = em.unwrap(SessionImplementor.class).getListeners();
assertEquals(
"Explicit pre-insert event through hibernate.ejb.event.pre-insert does not work",
eventListeners.getPreInsertEventListeners().length,

View File

@ -8,6 +8,8 @@ import java.util.Properties;
import javax.persistence.spi.ClassTransformer;
import javax.persistence.spi.PersistenceUnitInfo;
import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.persistence.Caching;
import javax.persistence.ValidationMode;
import javax.sql.DataSource;
import org.hibernate.cfg.Environment;
@ -71,6 +73,10 @@ public class PersistenceUnitInfoImpl implements PersistenceUnitInfo {
return properties;
}
public String PersistenceXMLSchemaVersion() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public ClassLoader getClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
@ -87,6 +93,16 @@ public class PersistenceUnitInfoImpl implements PersistenceUnitInfo {
return true;
}
public Caching getCaching() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public ValidationMode getValidationMode() {
//FIXME
return null; //To change body of implemented methods use File | Settings | File Templates.
}
public void addTransformer(ClassTransformer transformer) {
}

View File

@ -72,7 +72,7 @@ public class CascadePersistTest extends TestCase {
c2.setB2( anotherB2 );
}
}
Statistics statistics = ( (Session) em.getDelegate() ).getSessionFactory().getStatistics();
Statistics statistics = em.unwrap(Session.class).getSessionFactory().getStatistics();
statistics.setStatisticsEnabled( true );
statistics.clear();
em.persist( c2 );

View File

@ -271,7 +271,7 @@ public class FlushAndTransactionTest extends TestCase {
em.flush();
em.clear();
book.setName( "kitty kid2"); //non updated version
( (Session) em.getDelegate() ).update( book );
em.unwrap( Session.class ).update( book );
try {
em.getTransaction().commit();
fail( "Commit should be rollbacked" );

View File

@ -70,8 +70,9 @@
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<groupId>org.hibernate.java-persistence</groupId>
<artifactId>jpa-api</artifactId>
<version>2.0.Beta1</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>

View File

@ -24,14 +24,14 @@
package org.hibernate.envers;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import org.hibernate.Session;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.envers.event.AuditEventListener;
import org.hibernate.envers.exception.AuditException;
import org.hibernate.envers.reader.AuditReaderImpl;
import static org.hibernate.envers.tools.ArraysTools.arrayIncludesInstanceOf;
import org.hibernate.Session;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.event.EventListeners;
import org.hibernate.event.PostInsertEventListener;
@ -81,17 +81,11 @@ public class AuditReaderFactory {
* listeners aren't installed.
*/
public static AuditReader get(EntityManager entityManager) throws AuditException {
if (entityManager.getDelegate() instanceof Session) {
return get((Session) entityManager.getDelegate());
}
if (entityManager.getDelegate() instanceof EntityManager) {
entityManager = (EntityManager) entityManager.getDelegate();
if (entityManager.getDelegate() instanceof Session) {
return get((Session) entityManager.getDelegate());
}
}
throw new AuditException("Hibernate EntityManager not present!");
try {
return get( entityManager.unwrap(Session.class) );
}
catch ( PersistenceException e ) {
throw new AuditException("Hibernate EntityManager not present!");
}
}
}

View File

@ -25,6 +25,7 @@
package org.jboss.envers;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import org.hibernate.envers.event.AuditEventListener;
import org.hibernate.envers.reader.AuditReaderImpl;
@ -81,16 +82,11 @@ public class VersionsReaderFactory {
* listeners aren't installed.
*/
public static VersionsReader get(EntityManager entityManager) throws VersionsException {
if (entityManager.getDelegate() instanceof Session) {
return get((Session) entityManager.getDelegate());
}
if (entityManager.getDelegate() instanceof EntityManager) {
if (entityManager.getDelegate() instanceof Session) {
return get((Session) entityManager.getDelegate());
}
}
throw new VersionsException("Hibernate EntityManager not present!");
try {
return get( entityManager.unwrap(Session.class) );
}
catch ( PersistenceException e ) {
throw new VersionsException("Hibernate EntityManager not present!");
}
}
}

View File

@ -25,6 +25,7 @@ package org.hibernate.envers.test.integration.flush;
import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.entities.StrTestEntity;
@ -45,18 +46,12 @@ public abstract class AbstractFlushTest extends AbstractEntityTest {
}
private static Session getSession(EntityManager em) {
Object delegate = em.getDelegate();
if (delegate instanceof Session) {
return (Session) delegate;
} else if (delegate instanceof EntityManager) {
Object delegate2 = ((EntityManager) delegate).getDelegate();
if (delegate2 instanceof Session) {
return (Session) delegate2;
}
}
throw new RuntimeException("Invalid entity manager");
try {
return em.unwrap( Session.class );
}
catch ( PersistenceException e ) {
throw new RuntimeException("Invalid entity manager", e);
}
}
@BeforeClass(dependsOnMethods = "init")

View File

@ -100,7 +100,7 @@ public class HsqlTest {
entityManager.getTransaction().begin();
Session sesion = (Session) entityManager.getDelegate();
Session sesion = entityManager.unwrap(Session.class);
System.out.println(sesion.createQuery(
"select e from org.hibernate.envers.demo.Person_versions e " +
"where " +