-->
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: Repeated column in mapping for entity & composite identifier
PostPosted: Thu Nov 08, 2012 5:56 am 
Newbie

Joined: Thu Nov 08, 2012 5:20 am
Posts: 1
When running the setup below, I get the following error:
Code:
org.hibernate.MappingException: Repeated column in mapping for entity: com.company.product.datamodel.Cdo column: session_id (should be mapped with insert="false" update="false")
   at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:682)
   at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:704)
   ...


It is clear to me what causes the error, but I have no idea how to avoid it. What is the best way to model this structure?

Thank you very much in advance for you answer!

Setup
Database:
Code:
CREATE TABLE `session` (
  `id` int(11) NOT NULL,
  `name` varchar(120) DEFAULT NULL,
  `ref_date` date DEFAULT NULL,
  PRIMARY KEY (`id`)
)

CREATE TABLE `currency` (
  `session_id` int(11) NOT NULL,
  `id` int(11) NOT NULL,
  `code` char(3) DEFAULT NULL,
  `rate2eur` decimal(24,12) DEFAULT NULL,
  PRIMARY KEY (`session_id`,`id`)
)

CREATE TABLE `instrument` (
  `session_id` int(11) NOT NULL,
  `id` int(11) NOT NULL,
  `name` varchar(45) NOT NULL,
  `currency_id` int(11) NOT NULL,
  PRIMARY KEY (`session_id`,`id`)
)


Java (replaced geters and setters with ... for simplification):
Code:
@Entity
@Table(name="session")
public class Session {
   @javax.persistence.Id
   private int id;
   @Column(name="name")
   private String name;
   @Column(name="ref_date")
   private Date referenceDate;
   @OneToMany(mappedBy="session")
   private List<Instrument> instruments;
   
   ...
}

@Embeddable
public class DbId implements Serializable {
   private static final long serialVersionUID = 1L;
   
   @Column(name="id")
   private int id;
   @Column(name="session_id")
   private int sessionId;

   ...
   
   @Override
   public boolean equals(Object obj) {
      if(!(obj instanceof DbId))
         return false;
      DbId idObj = (DbId) obj;
      return id == idObj.getId() && sessionId == idObj.getSessionId();
   }
   
   @Override
   public int hashCode() {
      return id * 31 + sessionId;
   }
}

@Entity
@Table(name="currency")
public class Currency {
   @EmbeddedId
   @AttributeOverrides( {
      @AttributeOverride(name="id", column = @Column(name="id") ),
      @AttributeOverride(name="sessionId", column = @Column(name="session_id") )
   } )
   private DbId id;
   
   @Column(name="code")
   private String name;
   
   @Column(name="rate2eur")
   private Double rate2Eur;
   
   ...
}

@Entity
@Table(name="instrument")
public class Instrument {
   @EmbeddedId
   @AttributeOverrides( {
      @AttributeOverride(name="id", column = @Column(name="id") ),
      @AttributeOverride(name="sessionId", column = @Column(name="session_id") )
   } )
   private DbId id;
   @Column(name="name")
   private String name;
   @ManyToOne
   @JoinColumn(name="session_id", insertable=false, updatable=false)
   private Session session;
   @ManyToOne
   @JoinColumns({
      @JoinColumn(name="currency_id", referencedColumnName="id"),
      @JoinColumn(name="session_id", referencedColumnName="session_id")
   })
   private Currency currency;

   ...
}


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.