开发者

Extend entity classes with composite keys in hibernate

In our company we have a strange database model which can't be modified because to many systems works with them. Up to know we have a straight java application which connects with hibernate to the database and loads the data. We have for each table one xml mapping file.

The strange thing about the database is that we do not have any primary keys. Most table have a unique index containing several columns.

Now we want to use an application server (jboss) and the ejb model. So I created a class like this:

   @Entity
   @Table (name = "eakopf_t")
   public class Eakopf implements Serializable {

       @Embeddable
       public static class EakopfId implements Serializable { 

            private String mandant;

            private String fk_eakopf_posnr;

            // I removed here the getters and setters to shorten it up

       }

       @Id
       private EakopfId id;

       private String login;

       // I removed开发者_如何转开发 the getters and setters here as well    
   }

This works perfect.

Because our customers have different versions of the database schema I thought about extending this class on each database release change. So each interface we create with java can decide which version of the table will be used.

Here is the extended table class

   @Entity
   @Table (name = "eakopf_t")
   public class Eakopf6001 extends Eakopf implements Serializable {

        private String newField;

        // getters and setters
   }

If I use Eakopf (the base version) it is working if I do something like that:

EakopfId id = new EakopfId();
id.setMandant("001");
id.setFk_eakopf_posnr("ABC");
Eakopf kopf = (Eakopf) em.find(Eakopf.class, id);

But if I do this:

EakopfId id = new EakopfId();
id.setMandant("001");
id.setFk_eakopf_posnr("ABC");
Eakopf6001 kopf = (Eakopf6001) em.find(Eakopf6001.class, id);

this exception occues

javax.ejb.EJBException: javax.persistence.PersistenceException: 
org.hibernate.WrongClassException: Object with id: 
de.entity.Eakopf$EakopfId@291bfe83 was not of the specified subclass:  
de.entity.Eakopf (Discriminator: null)

Does anybody has an idea?

many greetings, Hauke


Doing what you did means to Hibernate that you're storing two different kinds of entities in a single table. This is possible is you use a discriminator column. But if I understand correctly, you just want one kind of entity in the table : Eakopf6001. In this case, its base class should be annotated with @MappedSuperClass, not with @Entity.

I would suggest creating a class annotated with @MappedEntity (let's call it BaseEakopf), and two entities: EaKopf and EaKopf6001, each with their set of additional fields. Include one of the other of the entities in the list of mapped classes, depending on which one you want to use.

My personal opinion is that if you have multiple versions of your app, they should use the same entities, but with different fields. Your version control system would take care of these multiple versions, rather than your source code (i.e. have one set of source files per version of the app, rather than one single set of source files for all the possible versions).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜