-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Override CollectionTables in MappedSuperclass??
PostPosted: Fri Jul 23, 2010 12:48 am 
Newbie

Joined: Fri Feb 05, 2010 11:34 pm
Posts: 7
Is there a way to do this with JPA 2.0?

I have multiple entities subclassing a MappedSuperclass. I can use dynamically generated orm.xml documents (as below) to assign database tables to my entities. However, these entities have ElementCollections and associated CollectionTables. How can I assign the collection table names?

The example below works fine if I name my collection tables "entitya_items" and "entityb_items". However, in my application I don't always know which entity (A or B -- they have different semantics) will be mapped to a given database table (and its corresponding collection table).

--Steve


Code:
@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Parent {

   @Id
   private Integer id;
   @ElementCollection
   private Set<String> items = new HashSet<String>();
}

public class EntityA extends Parent {

}

public class EntityB extends Parent {

}

   public static void main(String[] args) {
      
      Properties properties = new Properties();
      properties.put(Environment.DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
      Ejb3Configuration ejb3Configuration = new Ejb3Configuration().configure("test", properties);       
       
      PGSimpleDataSource dataSource = new PGSimpleDataSource();
      dataSource.setDatabaseName(DB);
      dataSource.setUser(USERNAME);
      dataSource.setPassword(PASSWORD);
      ejb3Configuration.setDataSource(dataSource);

      String orm = getORM(
              EntityA.class.getPackage().getName(),
              EntityA.class.getSimpleName(),
              "table_a");
      ejb3Configuration.addInputStream(new ByteArrayInputStream(orm.getBytes()));
       
      orm = getORM(
              EntityB.class.getPackage().getName(),
              EntityB.class.getSimpleName(),
              "table_b");      
      ejb3Configuration.addInputStream(new ByteArrayInputStream(orm.getBytes()));
       
      EntityManagerFactory emf = ejb3Configuration.buildEntityManagerFactory();
      EntityManager em = emf.createEntityManager();

      Object a = em.find(EntityA.class, Integer.valueOf(0));
      System.out.println("Retrieved: " + a);
      Object b = em.find(EntityB.class, Integer.valueOf(0));
      System.out.println("Retrieved: " + b);
      
      em.close();
      emf.close();
   }
   
   private static final String EOL = "\n";
   public static String getORM(String entityPackage, String entityName, String tableName) {
      String s = new StringBuilder()
         .append("<?xml version='1.0' encoding='UTF-8'?>" + EOL)
         .append("<entity-mappings " + EOL)
         .append("    xmlns='http://java.sun.com/xml/ns/persistence/orm' " + EOL)
         .append("    xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" + EOL)
         .append("    xsi:schemaLocation='http://java.sun.com/xml/ns/persistence/orm " + EOL)
         .append("        http://java.sun.com/xml/ns/persistence/orm_2_0.xsd'" + EOL)
         .append("    version='2.0'>" + EOL)
         .append("    <package>" + entityPackage + "</package>" + EOL)
         .append("    <access>FIELD</access>" + EOL)
         .append("    <entity class='" + entityName + "'>" + EOL)
         .append("        <table name='" + tableName + "'/>" + EOL)
         .append("    </entity>" + EOL)
         .append("</entity-mappings>" + EOL)
         .toString();
      return s;
   }

   


Top
 Profile  
 
 Post subject: Re: Override CollectionTables in MappedSuperclass??
PostPosted: Fri Jul 23, 2010 1:28 am 
Newbie

Joined: Tue Jul 20, 2010 1:53 am
Posts: 18
'ejb3Configuration' this related Obj in our code ,and must mapping with your Database table Obj .. There is needed to bother about your entities in programming code.


I think that this answer satisfy you ...


Top
 Profile  
 
 Post subject: Re: Override CollectionTables in MappedSuperclass??
PostPosted: Fri Jul 23, 2010 12:30 pm 
Newbie

Joined: Fri Feb 05, 2010 11:34 pm
Posts: 7
The use of dynamic orm.xml documents is a red herring; these could be static configuration files, and the problem remains. The question is how to use a MappedSuperclass to define a family of entities (same schema, different data) with collection properties, without hard-coding Java class names into the collection table names?

Here's what doesn't work:
  • Moving the collection properties from the superclass to the entity class -- defeats the purpose of the superclass as a uniform interface.
  • Naming the entity after the table instead of the entity class -- Hibernate still uses the entity class name in the collection table name.
  • Configuring the superclass fields in the <entity> configuration -- Hibernate ignores it.
  • Using <association-override> on the entity -- Hibernate rejects it ("Illegal attempt to define a @JoinColumn with a mappedBy association")
  • Use OneToMany instead of ElementCollection -- you can't associate the item's MappedSuperclass to the collection's MappedSuperclass; you must associate to a specific Entity ("@ManyToOne on EntityItemA.coll references an unknown entity").
  • Use OneToMany with <association-override> on the item entity -- there's no way to override the Entity association of the item's MappedSuperclass.

This seems like an oversight in JPA 2.0. According to the JPA spec, naming the entity the same as the table should work, but Hibernate ignores it. Perhaps this should be a JIRA item.

--Steve


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.