Mapping Manytomany with Composite Primary Key and Annotation:

Mapping ManyToMany with composite Primary key and Annotation:

I solved this issue. I mapped Getter instead of field.

public class StudentTClass {

//@EmbeddedId
private StudentTClassPK pk = new StudentTClassPK();

@EmbeddedId
public StudentTClassPK getPk() {
return pk;
}

jpa many to many with additional column and composite key

Resolved , the syntax of the AssociationOverride annotation was wrong

Correct syntax :

AssociationOverrides({
@AssociationOverride(name = "folder_documents_compositeKey.folder", joinColumns = @JoinColumn(name = "folder_id")),
@AssociationOverride(name = "folder_documents_compositeKey.document", joinColumns = {
@JoinColumn(name = "doc_id" , referencedColumnName = "id") ,
@JoinColumn(name = "matricule" , referencedColumnName = "matricule") })})

Many-to-many relationship with composite keys, extra column in Hibernate

Done. I change the following:

In Association class:

@AssociationOverrides({
@AssociationOverride(name = "idPk.appPk", joinColumns = { @JoinColumn(name = "ID_APP", referencedColumnName = "ID_APP"),
@JoinColumn(name = "VERSION", referencedColumnName = "VERSION") }),
@AssociationOverride(name = "idPK.customerPk", joinColumns = @JoinColumn(name = "ORG_ID"))
})


How do I map a One To Many Relationship with Composite Primary Key in JPA (Hibernate)?

These relationships should be mapped a bit differently:

@Entity
@Table("SCHOOL")
public School {
@Column(name = "SCHOOL_ID")
private String schoolId;

@OneToMany(mappedBy="school")
private List<Student> students;
}

@Entity
@Table("STUDENT")
public Student {
@EmbeddedId
private StudentPK studentPK;

@ManyToOne
@MapsId("schoolId")
private School school;
}

@Embeddable
public StudentPK implements Serializable {
@Column(name = "SCHOOL_ID")
private String schoolId;

@Column(name = "STUDENT_ID"
private String studentId;
}

Note the @OneToMany.mappedBy attribute on School.students and @MapsId annotation on Student.school.

Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.

Element mapping with composite primary key with Annotations

This is how I would try to do it (I'll be using simple classes in the code, hopefully super classes and interfaces aren't relevant to the issue).

There are two options. First one, the easier one, is if that "irrelevant additional data" in JOIN_TABLE is really irrelevant to the model.

@Entity
@Table(name="MAIN_TABLE")
public class MainTable {
@Id
@Type(type = "LongToNull")
private long id;

@ManyToMany
@JoinTable(name = "JOIN_TABLE", joinColumns = @JoinColumn(name = "JOIN_TABLE_ID"),
inverseJoinColumns = @JoinColumn(name = "THIRD_TABLE_ID")
private List<ThirdTable> thirdTableRecords = new ArrayList<ThirdTable>();
}

@Entity
@Table(name="THIRD_TABLE")
public class ThirdTable {
@Id
@Type(type = "LongToNull")
private long id;

@ManyToMany(mappedBy = "thirdTableRecords")
private List<MainTable> mainTableRecords = new ArrayList<MainTable>();

...
}

If "irrelevant additional data" still has to be modeled, then entities would look something like this

@Entity
@Table(name="MAIN_TABLE")
public class MainTable {
@Id
@Type(type = "LongToNull")
private long id;

@OneToMany(mappedBy = "mainTable")
private List<JoinTable> joinTableRecords = new ArrayList<JoinTable>();
}

@Entity
@Table(name="THIRD_TABLE")
public class ThirdTable {
@Id
@Type(type = "LongToNull")
private long id;

@OneToMany(mappedBy = "thirdTable")
private List<JoinTable> joinTableRecords = new ArrayList<joinTable>();

...
}

@Entity
@Table(name="JOIN_TABLE")
public class JoinTable {
@EmbeddedId
private JoinTablePK pkId;

private String irrelevantData;

@ManyToOne
@JoinColumn(name = "MAIN_TABLE_ID", insertable = false, updateable = false)
private MainTable mainTable;

@ManyToOne
@JoinColumn(name = "THIRD_TABLE_ID", insertable = false, updateable = false)
private ThirdTable thirdTable;

...
}

@Embeddable
public class JoinTablePK {
@Column(name = "MAIN_TABLE_ID")
private Long mainTableId;

@Column(name = "THIRD_TABLE_ID")
private Long thirdTableId;

// getters and setters
}

insertable = false and updateable = false are there because you are mapping two fields to the same column, and there can be only one writable field for one column.

I didn't test the code, so I would be surprised if it would work as it is, but hopefully it does give you some guidelines to follow, and you'll get at least a few steps closer to the solution.

Many to many mapping with jointable and composite key

inverseJoinColumns = {@JoinColumn(name = "theFirstColumnInTheJoinTable", 
referencedColumnName = "theFirstColumnInTheTargetTable"),
@JoinColumn(name = "theSecondColumnInTheJoinTable",
referencedColumnName = "theSecondColumnInTheTargetTable")}

But I would definitely avoid composite PKs: they make life much more complicated, and performance worse.

Many-to-many relationship with composite keys in Hibernate

Your join table needs 4 columns. 2 to reference the PK columns of example_table and 2 other to reference the PK columns of some_type_table.

So it should look like

+----------------+  +----------------------------------+  +----------------+      
| example_table | | example_table_to_some_type_table | | some_type_table|
+----------------+ +----------------------------------+ +----------------+
|example_table_id| |example_table_id | |some_type_id |
|another_id | |example_table_another_id | |another_id |
|... | |some_type_id | |... |
| | |some_type_table_another_id | | |
|other columns | +----------------------------------+ |other columns |
|... | |... |
+----------------+ +----------------+


Related Topics



Leave a reply



Submit