-->
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: OneToOne to an abstract Class
PostPosted: Thu Sep 10, 2009 4:24 am 
Newbie

Joined: Thu Sep 10, 2009 4:08 am
Posts: 3
Hi, I have to map a legacy DB and I'm in trouble with this.

I have a Contratto (C) class that inherits from abstract PreventivoContratto (PC).
(PC) has an id.
(C) has a composite natural key.

A (C) can have a Movimento (M), which is an abstract class with two implementations, Pagamento (PG) and Rimborso (R).

These are the mapping I'm trying to use now:

@Entity
@Table(name = "PTF_CONTRATTO", uniqueConstraints = @UniqueConstraint(columnNames = {
"NUM_N_POLIZZA", "NUM_N_VERSIONE" }))
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "FLAG_X_TRASFORMATO", discriminatorType = DiscriminatorType.CHAR)
@DiscriminatorValue(value = "U")
public abstract class PreventivoContratto {
@Id
@Column(name = "ID_N_POLIZZA")
long id;
}

(BTW: I had to add @DiscriminatorValue(value = "U") to make this work, even if the DiscriminatorColumn is a boolena T/F)

@Entity
@DiscriminatorValue(value="T")
public class Contratto extends PreventivoContratto {

@Column(name = "NUM_N_POLIZZA")
long numeroPolizza;

@Column(name = "NUM_N_VERSIONE")
int versionePolizza;

@OneToOne(mappedBy = "contratto")
Movimento movimento;
}

@MappedSuperclass
public abstract class Movimento {
@Id
long id;
public abstract long getId();

@OneToOne(optional = false)
@JoinColumns( {
@JoinColumn(name = "NUM_N_POLIZZA", referencedColumnName = "NUM_N_POLIZZA"),
@JoinColumn(name = "NUM_N_VERSIONE", referencedColumnName = "NUM_N_VERSIONE") })
Contratto contratto;
}

@Entity
@Table(name = "PTF_PAGAMENTO")
@AttributeOverride(name = "id", column = @Column(name = "ID_N_PAGAMENTO"))
public class Pagamento extends Movimento {
@Override
public long getId() {
return id;
}
}

I get this error:

Caused by: org.hibernate.AnnotationException: Unknown mappedBy in: Contratto.movimento, referenced property unknown: Movimento.contratto
at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:129)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1177)

(PC) a (C) resides on the same table, whereas (PG) and (R) have separate tables (I think the mappings says it all).

I understand the error says that (M) is not an Entity, but this is not correct.
Even if I map it as an entity, I get a "broken mapping exception".

Looking at hibernate source code, I understand this is because the OneToOne expects the number of columns mapped equals the number of columns in the mapped PK, but this is an error.

I could map the whole thing?

I even tried to use and Embedded, but it seems not to work with inherited classes.

Please help!

Tnx.

Dario


Top
 Profile  
 
 Post subject: Re: OneToOne to an abstract Class
PostPosted: Thu Sep 10, 2009 8:22 am 
Beginner
Beginner

Joined: Fri Jun 26, 2009 6:59 am
Posts: 23
on your database, what is the link from Contratto to Pagamento/Rimborso and vice versa, you should be using MappedBy with an ID.


Top
 Profile  
 
 Post subject: Re: OneToOne to an abstract Class
PostPosted: Thu Sep 10, 2009 8:48 am 
Newbie

Joined: Thu Sep 10, 2009 4:08 am
Posts: 3
Pagamento is mapped on table PTF_PAGAMENTO
Rimborso is mapped on table PTF_RIMBORSO

Both tables have columns NUM_N_POLIZZA and NUM_N_VERSIONE which form the natural composite non-primary key in table PTF_CONTRATTO (the table that holds the PreventivoContratto <- Contratto hierarchy).

Table PTF_CONTRATTO has no references to the other 2 tables.

I understand the problem is Hibernate is not able to map to somethine different than the PK: is this correct?
How can work this around?

Tnx again.


Top
 Profile  
 
 Post subject: Re: OneToOne to an abstract Class
PostPosted: Thu Sep 10, 2009 8:50 am 
Beginner
Beginner

Joined: Fri Jun 26, 2009 6:59 am
Posts: 23
hibernate is able to map foreign keys and composite keys, you're thinking the right sort of way for Object Relational Mapping, but you need to tell it how to identify which instance(s) of R or P is linked to the specific C.


Top
 Profile  
 
 Post subject: Re: OneToOne to an abstract Class
PostPosted: Thu Sep 10, 2009 9:04 am 
Newbie

Joined: Thu Sep 10, 2009 4:08 am
Posts: 3
Masterplan79th wrote:
hibernate is able to map foreign keys and composite keys, you're thinking the right sort of way for Object Relational Mapping, but you need to tell it how to identify which instance(s) of R or P is linked to the specific C.


Yes, it can map FK and composite keys, but this case is different: the PK of the referenced tabel is single column, whereas the FK refers to a different unique couple of columns.

How can I make Hibernate work in this situation?


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.