Skip to content

Commit

Permalink
HHH-7322 Refactoring of unidirectional one to many test to cater for …
Browse files Browse the repository at this point in the history
…hbm and annotations

Adding some error handling in Binder for the case that not all configuration sources are added
Upgrading JBoss logging
  • Loading branch information
hferentschik committed Aug 31, 2012
1 parent 22d6c78 commit d7fdf27
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 56 deletions.
8 changes: 2 additions & 6 deletions build.gradle
Expand Up @@ -6,8 +6,7 @@ allprojects {
repositories {
mavenCentral()
mavenLocal()



mavenRepo name: 'jboss-nexus', url: "http://repository.jboss.org/nexus/content/groups/public/"
mavenRepo name: "jboss-snapshots", url: "http://snapshots.jboss.org/maven2/"
}
Expand All @@ -17,8 +16,7 @@ buildscript {
repositories {
mavenCentral()
mavenLocal()



mavenRepo name: 'jboss-nexus', url: "http://repository.jboss.org/nexus/content/groups/public/"
mavenRepo name: "jboss-snapshots", url: "http://snapshots.jboss.org/maven2/"
}
Expand Down Expand Up @@ -149,12 +147,10 @@ subprojects { subProject ->
"-source", "1.6",
"-target", "1.6",
"-AtranslationFilesPath=${project.rootDir}/src/main/resources"

]
);
outputs.dir sourceSets.main.generatedLoggingSrcDir;
doFirst {
// source = sourceSets.main.originalJavaSrcDirs
sourceSets.main.generatedLoggingSrcDir.mkdirs()
}
}
Expand Down
Expand Up @@ -1575,6 +1575,8 @@ void cannotResolveNonNullableTransientDependencies(String transientEntityString,
@Message(value = "NaturalId queries executed to database: %s", id = 442)
void naturalIdQueriesExecuted(long naturalIdQueriesExecutionCount);

@Message(value = "Unable to find mapping information for %s. Are you sure all annotated classes and configuration files are added?", id = 443)
String missingEntitySource(String entityName);

// moved from hibernate-entitymanager ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
Expand Up @@ -47,6 +47,7 @@
import org.hibernate.id.IdentityGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.id.factory.IdentifierGeneratorFactory;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.internal.util.ValueHolder;
Expand Down Expand Up @@ -165,6 +166,10 @@
*/
public class Binder {
private static final Logger log = Logger.getLogger( Binder.class );
private static final CoreMessageLogger coreLogger = Logger.getMessageLogger(
CoreMessageLogger.class,
Binder.class.getName()
);

private static org.hibernate.internal.util.ValueHolder< Class< ? >> createSingularAttributeJavaType(
final Class< ? > attributeContainerClassReference,
Expand Down Expand Up @@ -1993,6 +1998,11 @@ private EntityBinding entityBinding( final String entityName ) {
if ( entityBinding == null ) {
// Find appropriate source to create binding
final EntitySource entitySource = entitySourcesByName.get( entityName );
if(entitySource == null) {
String msg = coreLogger.missingEntitySource( entityName );
throw new MappingException( msg, null );
}

// Get super entity binding (creating it if necessary using recursive call to this method)
final EntityBinding superEntityBinding =
SubclassEntitySource.class.isInstance( entitySource )
Expand Down
Expand Up @@ -46,7 +46,6 @@
import org.hibernate.metamodel.spi.relational.Identifier;
import org.hibernate.metamodel.spi.relational.JdbcDataType;
import org.hibernate.metamodel.spi.relational.Value;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.internal.StandardServiceRegistryImpl;
import org.hibernate.testing.junit4.BaseUnitTestCase;
Expand All @@ -64,9 +63,9 @@
* Basic tests of {@code hbm.xml} and annotation binding code
*
* @author Steve Ebersole
* @author Hardy Ferentschik
*/
public abstract class AbstractBasicBindingTests extends BaseUnitTestCase {

private StandardServiceRegistryImpl serviceRegistry;

@Before
Expand All @@ -79,10 +78,6 @@ public void tearDown() {
serviceRegistry.destroy();
}

protected ServiceRegistry basicServiceRegistry() {
return serviceRegistry;
}

@Test
public void testSimpleEntityMapping() {
MetadataSources sources = new MetadataSources( serviceRegistry );
Expand Down
Expand Up @@ -34,15 +34,13 @@
import org.junit.Test;

import org.hibernate.engine.FetchTiming;
import org.hibernate.metamodel.MetadataSourceProcessingOrder;
import org.hibernate.metamodel.MetadataSources;
import org.hibernate.metamodel.internal.MetadataImpl;
import org.hibernate.metamodel.spi.binding.EntityBinding;
import org.hibernate.metamodel.spi.binding.HibernateTypeDescriptor;
import org.hibernate.metamodel.spi.binding.PluralAttributeBinding;
import org.hibernate.metamodel.spi.binding.PluralAttributeElementBinding;
import org.hibernate.metamodel.spi.binding.PluralAttributeKeyBinding;
import org.hibernate.metamodel.spi.binding.SimpleEntity;
import org.hibernate.metamodel.spi.binding.SingularAttributeBinding;
import org.hibernate.metamodel.spi.relational.Column;
import org.hibernate.metamodel.spi.relational.ForeignKey;
Expand All @@ -67,42 +65,33 @@
/**
* @author Gail Badner
*/
public class UnidirectionalOneToManyBindingTests extends BaseUnitTestCase {
public abstract class AbstractUnidirectionalOneToManyBindingTests extends BaseUnitTestCase {
private StandardServiceRegistryImpl serviceRegistry;
private MetadataImpl metadata;

@Before
public void setUp() {
serviceRegistry = ( StandardServiceRegistryImpl ) new ServiceRegistryBuilder().buildServiceRegistry();
MetadataSources metadataSources = new MetadataSources( serviceRegistry );
addSources( metadataSources );
metadata = ( MetadataImpl ) metadataSources.getMetadataBuilder().buildMetadata();
}

@After
public void tearDown() {
serviceRegistry.destroy();
}

// @Test
// public void testAnnotations() {
// doTest( MetadataSourceProcessingOrder.ANNOTATIONS_FIRST );
// }
public abstract void addSources(MetadataSources sources);

@Test
public void testHbm() {
doTest( MetadataSourceProcessingOrder.HBM_FIRST );
}

private void doTest(MetadataSourceProcessingOrder processingOrder) {
MetadataSources sources = new MetadataSources( serviceRegistry );
// sources.addAnnotatedClass( EntityWithBasicCollections.class );
sources.addResource( "org/hibernate/metamodel/spi/binding/onetomany/EntityWithUnidirectionalOneToMany.hbm.xml" );
sources.addResource( "org/hibernate/metamodel/spi/binding/SimpleEntity.hbm.xml" );
MetadataImpl metadata = ( MetadataImpl ) sources.getMetadataBuilder().with( processingOrder ).buildMetadata();

public void testOneToMany() {
final EntityBinding entityBinding = metadata.getEntityBinding( EntityWithUnidirectionalOneToMany.class.getName() );
final EntityBinding simpleEntityBinding = metadata.getEntityBinding( SimpleEntity.class.getName() );
final EntityBinding simpleEntityBinding = metadata.getEntityBinding( ReferencedEntity.class.getName() );
assertNotNull( entityBinding );

assertEquals(
Identifier.toIdentifier( "SimpleEntity" ),
Identifier.toIdentifier( "ReferencedEntity" ),
simpleEntityBinding.getPrimaryTable().getLogicalName()
);
assertEquals( 1, simpleEntityBinding.getPrimaryTable().getPrimaryKey().getColumnSpan() );
Expand Down
@@ -0,0 +1,45 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @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.metamodel.spi.binding.onetomany;

import org.junit.Test;

import org.hibernate.metamodel.MetadataSources;
import org.hibernate.testing.FailureExpectedWithNewMetamodel;

/**
* @author Hardy Ferentschik
*/
public class AnnotationUnidirectionalOneToManyBindingTests extends AbstractUnidirectionalOneToManyBindingTests {
public void addSources(MetadataSources sources) {
sources.addAnnotatedClass( EntityWithUnidirectionalOneToMany.class );
sources.addAnnotatedClass( ReferencedEntity.class );
}

@Test
@FailureExpectedWithNewMetamodel
public void testOneToMany() {
super.testOneToMany();
}
}
Expand Up @@ -11,20 +11,18 @@
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.hibernate.metamodel.spi.binding.SimpleEntity;

/**
* @author Gail Badner
*/
@Entity
public class EntityWithUnidirectionalOneToMany {
private Long id;
private String name;
private Collection<SimpleEntity> theBag = new ArrayList<SimpleEntity>();
private Set<SimpleEntity> theSet = new HashSet<SimpleEntity>();
private List<SimpleEntity> theList = new ArrayList<SimpleEntity>();
private Map<String, SimpleEntity> theMap = new HashMap<String, SimpleEntity>();
private Collection<SimpleEntity> thePropertyRefBag = new ArrayList<SimpleEntity>();
private Collection<ReferencedEntity> theBag = new ArrayList<ReferencedEntity>();
private Set<ReferencedEntity> theSet = new HashSet<ReferencedEntity>();
private List<ReferencedEntity> theList = new ArrayList<ReferencedEntity>();
private Map<String, ReferencedEntity> theMap = new HashMap<String, ReferencedEntity>();
private Collection<ReferencedEntity> thePropertyRefBag = new ArrayList<ReferencedEntity>();

@Id
public Long getId() {
Expand All @@ -44,47 +42,47 @@ public void setName(String name) {
}

@OneToMany
public Collection<SimpleEntity> getTheBag() {
public Collection<ReferencedEntity> getTheBag() {
return theBag;
}

public void setTheBag(Collection<SimpleEntity> theBag) {
public void setTheBag(Collection<ReferencedEntity> theBag) {
this.theBag = theBag;
}

@OneToMany
public Set<SimpleEntity> getTheSet() {
public Set<ReferencedEntity> getTheSet() {
return theSet;
}

public void setTheSet(Set<SimpleEntity> theSet) {
public void setTheSet(Set<ReferencedEntity> theSet) {
this.theSet = theSet;
}

@OneToMany
public List<SimpleEntity> getTheList() {
public List<ReferencedEntity> getTheList() {
return theList;
}

public void setTheList(List<SimpleEntity> theList) {
public void setTheList(List<ReferencedEntity> theList) {
this.theList = theList;
}

@OneToMany
public Map<String, SimpleEntity> getTheMap() {
public Map<String, ReferencedEntity> getTheMap() {
return theMap;
}

public void setTheMap(Map<String, SimpleEntity> theMap) {
public void setTheMap(Map<String, ReferencedEntity> theMap) {
this.theMap = theMap;
}

@OneToMany
public Collection<SimpleEntity> getThePropertyRefSet() {
public Collection<ReferencedEntity> getThePropertyRefSet() {
return thePropertyRefBag;
}

public void setThePropertyRefSet(Set<SimpleEntity> thePropertyRefSet) {
public void setThePropertyRefSet(Set<ReferencedEntity> thePropertyRefSet) {
this.thePropertyRefBag = thePropertyRefSet;
}
}
Expand Down
@@ -0,0 +1,36 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @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.metamodel.spi.binding.onetomany;

import org.hibernate.metamodel.MetadataSources;

/**
* @author Hardy Ferentschik
*/
public class HbmUnidirectionalOneToManyBindingTests extends AbstractUnidirectionalOneToManyBindingTests {
public void addSources(MetadataSources sources) {
sources.addResource( "org/hibernate/metamodel/spi/binding/onetomany/EntityWithUnidirectionalOneToMany.hbm.xml" );
sources.addResource( "org/hibernate/metamodel/spi/binding/onetomany/ReferencedEntity.hbm.xml" );
}
}
@@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @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.metamodel.spi.binding.onetomany;

import javax.persistence.Entity;
import javax.persistence.Id;

/**
* @author Hardy Ferentschik
*/
@Entity
public class ReferencedEntity {
@Id
private Long id;
private String name;

public ReferencedEntity() {
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

0 comments on commit d7fdf27

Please sign in to comment.