8317

One Id of composite key to be referenced in another entity

Question:

<ul><li>Table 1 has only two columns which form a composite key.</li> <li>Table 2 id column is being generated.</li> <li>Value for one property of Table1 I am supplying and for second property I need it to pick up the generated value of Table2. Here is what I have so far-</li> </ul>

Composite primary key:

public class PKClass implements Serializable { private static final long serialVersionUID = -2092390836751020965L; public PKClass(){} public PKClass(long actId, long actAttId){ this.actvId = actId; this.actvAtId = actAttId; } private long actvId; private long actvAtId; // getters, setters, equals, hashcode }

Entity class 1:

@Entity @Table(name = "Table1") @IdClass(PKClass.class) public class AtLinkData implements Serializable { @Id @Column(name = "ACT_ID", nullable = false) private long actvId; @Id @Column(name = "ACT_AT_ID") private long actvAtId; @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, , mappedBy = "atLinkData") @JoinColumn(name = "ACT_AT_ID") private AtData atData; // getters, setters }

Entity class 2:

@Entity @Table(name = "Table2") public class AtData implements Serializable { @TableGenerator(name = "somename", table = "sometable", pkColumnName = "somecolumn", valueColumnName = "valuename", pkColumnValue = "Table2", allocationSize = 1) @Id @GeneratedValue(strategy = GenerationType.TABLE, generator = "somename") @Column(name = "ACT_AT_ID") private Long id; // other variables @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "ACT_AT_ID") private AtLinkData atLinkData; // getters setters }

I have been trying different things, but with the code above, I am getting the error:

Caused by: org.hibernate.MappingException: broken column mapping for: atLinkData.id of: com.test.database.AtData

Answer1:

@OneToOne(mappedBy="otherInfo") Use JoinColumns annotation not JoinColumn.

@OneToOne(fetch = FetchType.LAZY) @JoinColumns({ @JoinColumn(name = "ACTVID", referencedColumnName = "ACTVID"), @JoinColumn(name = "ACTVATID", referencedColumnName = "ACTVATID") }) private AtLinkData atLinkData;

DB table of AtData entity you have to add two foreign key column ACTVID and ACTVATID.

<strong>Update</strong>

If you cannot add new columns in Table 2, use mappedBy attribute at AtData entity, not AtLinkData. Try as below.

AtLinkData.java

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "ACT_AT_ID") private AtData atData;

AtData.java

@OneToOne(fetch = FetchType.LAZY, mappedBy="atData") private AtLinkData atLinkData;

Answer2:

Here is how I solved it:

AtLinkData.java

@Id @GeneratedValue(generator = "foreignGenerator") @org.hibernate.annotations.GenericGenerator(name = "foreignGenerator", strategy = "foreign", parameters = @Parameter(name = "property", value = "atData")) @Column(name = "ACT_AT_ID") private long actvAtId; @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinColumn(name = "ACT_AT_ID",insertable=false,updatable=false) private AtData atData; // getters, setters

AtData.java

@OneToOne(fetch = FetchType.LAZY, mappedBy="atData") private AtLinkData atLinkData;

Thanks <a href="https://stackoverflow.com/users/1573835/cycdemo" rel="nofollow">@CycDemo</a> for your help.

Recommend

  • Mapping a class that consists only of a composite PK without @IdClass or @EmbeddedId
  • what does Caret sign do in Dart
  • How HashMap works internally
  • Consistent HashCode for a List of String accross processes/platforms
  • javafx change style of a node frequently not by CSS
  • ES6 Maps and Sets: how are object keys indexed efficiently?
  • Verify there is a combination of unique string
  • GroovyString hashCode and equals do not evaluate to same value as String
  • equals method usage in string and list
  • Unexpected behaviour of EntityManager.merge()
  • Validate instance of domain class is unique
  • FindEntry function in Dictionary.cs
  • Why was hashcode designed to return int [duplicate]
  • Binding from items in ItemsControl to ItemControl's DataContext
  • Sortedset not using custom equals
  • getting unspecified error in docusign API while using COMPOSITE template
  • Check if Set of Object contain an Object with this attribute
  • Approach to Incrementally Index Database Data from Multi-Table Join in Lucene with No Unique Key
  • What Java collection considers permutations to be equal?
  • hascode and equals methods not overridden - How the put and get will work?
  • How to use @JsonIdentityInfo with composite PK?
  • Mutation of the keys in HashMap causes wrong results
  • Hashmap put(), is it always ordering?
  • Java: equals and ==
  • using Natural order with treeset in java
  • How to use ES6 Hash Map on any object without maintaing a reference (I.e. Java hashcode)
  • Use @NodeEntity on interface/abstract class
  • Doctrine/Symfony entity generator and generating entity from one table
  • Why cepheus don't send int without quotes to orion?
  • GAE: Way to get reference to an HttpSession from its ID?
  • Force show.bind execution
  • Groovy: Unexpected token “:”
  • Replace value with Factor in r data.table
  • Handling un-mapped Rest path
  • How to access EntityManager inside Entity class in EJB3
  • Repeat a vertical line on every page in Report Builder / SSRS
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?