-->
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.  [ 5 posts ] 
Author Message
 Post subject: Discriminator column part of Primary Key, How To ?
PostPosted: Sun Jul 13, 2008 9:35 am 
Newbie

Joined: Thu Feb 14, 2008 10:43 pm
Posts: 8
I have a composed primary key, and one field (last field) is the discriminator column.

I use JPA Annotation and Hbn tell me "repeated column in mapping for entity".

I have read that discriminator column should be mapped "insert=false" like this:
Code:
<discriminator column="MyType" type="String" not-null="true" insert="false" />


But i haven't found out how to do it with annotation.

I have tried:
Code:
@Entity
@Table(name = "MyEntityTable")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="MyType", discriminatorType=DiscriminatorType.STRING, length = 4)

public abstract class MyEntity implements Serializable {
    @EmbeddedId
    @AttributeOverride(name = "myType", column = @Column(name = "MyType", nullable = false, insertable = false, updatable = false))
    public MyEntityPK myEntityPK;
...


but it doesn't work.

I have tried also a direct change of column definition in MyEntityPK (but i can't use this solution, because MyEntityPK is used also on other entity as embedded object, so all columns must be insert = True), but even this does not work and get the same error ("repeated column ...").

Solutions ?

Regards.

Ps
I use Hibernate Core 3.3.0.CR1 with EntityManager and Annotation 3.4.0.CR1


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 14, 2008 6:10 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

could you maybe provide a little more information, for example how all involved classes are annotated? Also what is the full stack trace of the exception?

Are you working against a legacy database or what else is the reason to make the discriminator column part of the primary key?

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 14, 2008 10:38 am 
Newbie

Joined: Thu Feb 14, 2008 10:43 pm
Posts: 8
hardy.ferentschik wrote:
Hi,

could you maybe provide a little more information, for example how all involved classes are annotated? Also what is the full stack trace of the exception?

Are you working against a legacy database or what else is the reason to make the discriminator column part of the primary key?

--Hardy


Yes, legacy DB.

The PK is like:

cdOwner int,
idObject int,
cdObjectType char(4)

We want the possibility of having two keys like 1, 1, ABCD and 1, 1, ZWXY.
Same Owner, same id but different Type (so 2 different object).

Code:
@Entity
@Table(name = "MyEntityTable")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="cdObjectType", discriminatorType=DiscriminatorType.STRING, length = 4)

public abstract class MyEntity implements Serializable {
    @EmbeddedId
    @AttributeOverride(name = "cdObjectType", column = @Column(name = "cdObjectType", nullable = false, insertable = false, updatable = false))
    public MyEntityPK myEntityPK;

...


Code:
@Embeddable
public class MyEntityPK implements Serializable {
    //@org.hibernate.validator.Min(value=1)
    //@org.hibernate.validator.NotNull
    @Column(name = "cdOwner", nullable = false)
    private Integer cdOwner;

    //@org.hibernate.validator.Min(value=1)
    //@org.hibernate.validator.NotNull   
    @Column(name = "idObject", nullable = false)
    private Integer idObject;
   
    //@org.hibernate.validator.Length(min=4, max=4)
    //@org.hibernate.validator.NotNull
    @Column(name="cdTkObjectType", length=4, nullable=false)
    private String cdTkObjectType;
   
    ...


Code:
@Entity
@DiscriminatorValue("CARS")
@SecondaryTable(
    name="MySecondaryTable",
    pkJoinColumns={
        @PrimaryKeyJoinColumn(name="cdOwner", referencedColumnName="cdOwner"),
        @PrimaryKeyJoinColumn(name="idObject", referencedColumnName="idObject"),
        @PrimaryKeyJoinColumn(name="cdObjectType", referencedColumnName="cdObjectType")
    }
)
public class Cars extends MyEntity implements Serializable {
    private static final long serialVersionUID = 1L;
   
    //@org.hibernate.validator.NotNull
    @Column(name = "dhSold", table="MySecondaryTable", nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date dhSold;

    ...


Error:

Code:
javax.persistence.PersistenceException: [PersistenceUnit: MyPU] Unable to build EntityManagerFactory
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
   at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
   at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:52)
   at it.myproject.StartUpClass.<init>(StartUpClass.java:146)
   at it.myproject.StartUpClass.main(StartUpClass.java:1401)
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: it.myproject.entities.Cars column: cdObjectType (should be mapped with insert="false" update="false")
   at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:647)
   at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:690)
   at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:445)
   at org.hibernate.mapping.SingleTableSubclass.validate(SingleTableSubclass.java:43)
   at org.hibernate.cfg.Configuration.validate(Configuration.java:1112)
   at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1297)
   at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:854)
   at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:669)
   ... 4 common frames omitted



Regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 14, 2008 3:05 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

it sounds a little like this issue: http://opensource.atlassian.com/projects/hibernate/browse/HB-1149.

It would be interesting to know if it is possible to configure your example using xml configuration. Have you tried? I am also wondering whether you could use @DiscriminatorFormula instead of DiscriminatorColumn. It might be worth a try.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 24, 2008 5:55 pm 
Newbie

Joined: Thu Feb 14, 2008 10:43 pm
Posts: 8
hardy.ferentschik wrote:

Hi,

yes it is the same problem.
It has been fixed for XML (with discriminator insert=false) but not for annotation (or the solution is unknown).

Quote:
It would be interesting to know if it is possible to configure your example using xml configuration. Have you tried? I am also wondering whether you could use @DiscriminatorFormula instead of DiscriminatorColumn. It might be worth a try.

--Hardy


I never have used xml files for jpa or hbn so i have no experience in using it.

I have tried @DiscriminatorFormula and it seem to work.

This code works:

Code:
@Entity
@Table(name = "MyEntityTable")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
//@DiscriminatorColumn(name="cdObjectType", discriminatorType=DiscriminatorType.STRING, length = 4)
@org.hibernate.annotations.DiscriminatorFormula(value="cdTkObjectType")

public abstract class MyEntity implements Serializable {
    @EmbeddedId
    public MyEntityPK myEntityPK;

...


but it uses specific hibernate annotation and furthermore i don't understand why it doesn't work with jpa @DiscriminatorColumn annotation


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.