-->
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.  [ 1 post ] 
Author Message
 Post subject: OneToMany with non-primary-key as foreign-key fails
PostPosted: Fri Jan 11, 2008 3:37 am 
Newbie

Joined: Fri Jan 11, 2008 3:00 am
Posts: 1
Hi,

We have a OneToMany bidirectional relation where the many-side (entity MerchantCurrency, cf. below) has a foreign key which does not reference a primary key field (in entity MerchantVertrag) but a unique key field. When loading the collection Hibernate uses the primary key field instead of the annotated unique field which ends up in an SQL error.

The code we wrote is as follows (excerpt):

Code:
@javax.persistence.Entity
@javax.persistence.Table(name="MERCHANT_VERTRAG")
public class MerchantVertrag implements Serializable {

   @javax.persistence.Id
   @javax.persistence.Column(name = "MERCHANTVERTRAG_ID")
   private Long merchantVertragId;

   @javax.persistence.Column(name = "MERCHANT")
   private String merchantExtId;

   @javax.persistence.OneToMany(cascade={javax.persistence.CascadeType.ALL}, fetch=javax.persistence.FetchType.LAZY, mappedBy="merchant")
   private Set<MerchantCurrency> merchantCurrencySet;

// rest of class not shown
}

@javax.persistence.Entity
@javax.persistence.Table(name="MERCHANT_CURRENCY")
@javax.persistence.IdClass(MerchantCurrencyPK.class)
public class MerchantCurrency implements Serializable {

   @Id
   @ManyToOne(optional=false, cascade={}, fetch=FetchType.LAZY)
   @JoinColumn(name="MERCHANT", referencedColumnName="MERCHANT")
   private MerchantVertrag merchant;
   
   @Id
   private Integer currencyId;

//rest of class not shown
}

class MerchantCurrencyPK implements Serializable {
   
   @Column(name = "MERCHANT")
   @org.hibernate.annotations.Type(type="org.hibernate.type.StringType")
   private MerchantVertrag merchant;
   
   @Column(name = "CURRENCY_ID")
   private Integer currencyId;

// rest of class not shown
}


One MerchantVertrag has n MerchantCurrency. MerchantCurrency has a composite key MerchantCurrencyPK where one part of it is a MerchantVertrag. The underlying DB schema for entity MerchantVertrag is as follows (excerpt):

Code:
CREATE TABLE MERCHANT_VERTRAG
(
  MERCHANTVERTRAG_ID          NUMBER(8)         NOT NULL,
  MERCHANT                    VARCHAR2(15 BYTE) NOT NULL,
  constraint PK_MV primary key (MERCHANTVERTRAG_ID),
  constraint PK_MV unique key (MERCHANT)
);


And for entity MerchantCurrency:

Code:
CREATE TABLE MERCHANT_CURRENCY
(
  MERCHANT     VARCHAR2(15 BYTE)                NOT NULL,
  CURRENCY_ID  NUMBER(3)                        NOT NULL,
  constraint PK_MC  primary key (CURRENCY_ID, MERCHANT)
);


According to Hibernate documentation the outlined code should work. When executing it, Hibernate fails with the following problem (log excerpt):

Code:
...
Hibernate:
    select
        merchantcu0_.MERCHANT as MERCHANT1_,
        merchantcu0_.CURRENCY_ID as CURRENCY1_1_,
        merchantcu0_.CURRENCY_ID as CURRENCY1_7_0_,
        merchantcu0_.MERCHANT as MERCHANT7_0_,
        merchantcu0_.MUTTS as MUTTS7_0_,
        merchantcu0_.MUT_UID as MUT4_7_0_
    from
        MERCHANT_CURRENCY merchantcu0_
    where
        merchantcu0_.MERCHANT=?
08:05:00.317 WARN  JDBCExceptionReporter: SQL Error: 1722, SQLState: 42000
08:05:00.317 ERROR JDBCExceptionReporter: ORA-01722: invalid number


It seems that Hibernate uses the primary key (merchantvertrag_id, which has type Long) as parameter to the MERCHANT field (which has actually type String) of table merchant_currency.

Does anyone have an idea what problem we are facing here?

Any help is appreciated.

Thanks,
Lukas


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

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.