From 39b098aac34937e7dc35b9ecc5b185ca972e8fca Mon Sep 17 00:00:00 2001 From: Scott Marlow Date: Tue, 5 Jan 2010 03:16:05 +0000 Subject: [PATCH] HHH-4753 Default table name for @CollectionTable is not inferred correctly according to spec requirement git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18405 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../test/annotations/namingstrategy/A.java | 68 +++++++++++++++++++ .../namingstrategy/AddressEntry.java | 45 ++++++++++++ .../namingstrategy/NamingStrategyTest.java | 37 +++++++++- 3 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/namingstrategy/A.java create mode 100644 annotations/src/test/java/org/hibernate/test/annotations/namingstrategy/AddressEntry.java diff --git a/annotations/src/test/java/org/hibernate/test/annotations/namingstrategy/A.java b/annotations/src/test/java/org/hibernate/test/annotations/namingstrategy/A.java new file mode 100644 index 0000000000..97ff350d50 --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/namingstrategy/A.java @@ -0,0 +1,68 @@ +package org.hibernate.test.annotations.namingstrategy; + + +import java.util.HashSet; +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.Table; +import javax.persistence.Id; +import javax.persistence.ElementCollection; +import javax.persistence.Column; +import javax.persistence.JoinColumn; +import javax.persistence.CascadeType; +import javax.persistence.CollectionTable; + +@Entity +@Table(name = "AEC") +public class A implements java.io.Serializable { + @Id + protected String id; + protected String name; + protected int value; + + @ElementCollection + protected Set address = new HashSet(); + + public A() { + } + + public A(String id, String name, int value) { + this.id = id; + this.name = name; + this.value = value; + } + + // Default to table A_AddressEntry + public Set getAddress() { + return address; + } + + public void setAddress(Set addr) { + this.address = addr; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getValue() { + return value; + } + + public void setValue(int val) { + this.value = val; + } +} diff --git a/annotations/src/test/java/org/hibernate/test/annotations/namingstrategy/AddressEntry.java b/annotations/src/test/java/org/hibernate/test/annotations/namingstrategy/AddressEntry.java new file mode 100644 index 0000000000..7db7f0774d --- /dev/null +++ b/annotations/src/test/java/org/hibernate/test/annotations/namingstrategy/AddressEntry.java @@ -0,0 +1,45 @@ +package org.hibernate.test.annotations.namingstrategy; + +import javax.persistence.Embeddable; + +@Embeddable +public class AddressEntry implements java.io.Serializable { + protected String street; + protected String city; + protected String state; + protected String zip; + + public AddressEntry() { + } + + public AddressEntry( String street, String city, String state, String zip) { + this.street = street; + this.city = city; + this.state = state; + this.zip = zip; + } + public String getCity() { + return city; + } + public void setCity(String c) { + city = c; + } + public String getState() { + return state; + } + public void setState(String state) { + this.state = state; + } + public String getStreet() { + return street; + } + public void setStreet(String street) { + this.street = street; + } + public String getZip() { + return zip; + } + public void setZip(String zip) { + this.zip = zip; + } +} \ No newline at end of file diff --git a/annotations/src/test/java/org/hibernate/test/annotations/namingstrategy/NamingStrategyTest.java b/annotations/src/test/java/org/hibernate/test/annotations/namingstrategy/NamingStrategyTest.java index ec5401252c..2298bcec8f 100644 --- a/annotations/src/test/java/org/hibernate/test/annotations/namingstrategy/NamingStrategyTest.java +++ b/annotations/src/test/java/org/hibernate/test/annotations/namingstrategy/NamingStrategyTest.java @@ -1,12 +1,16 @@ -// $Id:$ +// $Id$ package org.hibernate.test.annotations.namingstrategy; import java.io.PrintWriter; import java.io.StringWriter; +import java.util.Iterator; import junit.framework.TestCase; import org.hibernate.cfg.AnnotationConfiguration; +import org.hibernate.cfg.EJB3NamingStrategy; +import org.hibernate.cfg.Mappings; +import org.hibernate.mapping.Table; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,7 +38,36 @@ public class NamingStrategyTest extends TestCase { fail(e.getMessage()); } } - + + public void testWithEJB3NamingStrategy() throws Exception { + try { + AnnotationConfiguration config = new AnnotationConfiguration(); + config.setNamingStrategy(EJB3NamingStrategy.INSTANCE); + config.addAnnotatedClass(A.class); + config.addAnnotatedClass(AddressEntry.class); + config.buildSessionFactory(); + Mappings mappings = config.createMappings(); + boolean foundIt = false; + + for ( Iterator iter = mappings.iterateTables(); iter.hasNext(); ) { + Table table = (Table) iter.next(); + log.info("testWithEJB3NamingStrategy table = " + table.getName()); + if ( table.getName().equalsIgnoreCase("A_ADDRESS")) { + foundIt = true; + } + // make sure we use A_ADDRESS instead of AEC_address + assertFalse("got table name mapped to: AEC_address which violates JPA-2 spec section 11.1.8 ([OWNING_ENTITY_NAME]_[COLLECTION_ATTRIBUTE_NAME])",table.getName().equalsIgnoreCase("AEC_address")); + } + assertTrue("table not mapped to A_ADDRESS which violates JPA-2 spec section 11.1.8",foundIt); + } + catch( Exception e ) { + StringWriter writer = new StringWriter(); + e.printStackTrace(new PrintWriter(writer)); + log.debug(writer.toString()); + fail(e.getMessage()); + } + } + public void testWithoutCustomNamingStrategy() throws Exception { try { AnnotationConfiguration config = new AnnotationConfiguration();