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!
SetupDatabase:
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;
...
}