Hibernate @ManyToMany mapping with composite keys

I'm trying to map a ManyToMany relationships between 2 tables, both having composite primary keys

LSFOCTB which primary key is composed of : LSFOC_CODSOC,LSFOC_CODLSC,LSFOC_CODFOC

LSFORTB which primary key is composed of : LSFOR_CODSOC,LSFOR_CODLSC,LSFOC_CODFOR

The table in charge of the ManyToMany relationship is :

LSFCFTB, with : LSFCF_CODSOC,LSFCF_CODLSC,LSFCF_CODFOC,LSFCF_CODFOR

So, in the hibernate model mapping LSFOCTB, I tried :

@ManyToMany(targetEntity = package.LSFOCTB.class, cascade = { CascadeType.PERSIST,
            CascadeType.MERGE })
    @JoinTable(name = "LSFCFTB", joinColumns = {
            @JoinColumn(name = "LSFCF_CODLSC", referencedColumnName = "LSFOC_CODLSC"),
            @JoinColumn(name = "LSFCF_CODFOC", referencedColumnName = "LSFOC_CODFOC"),
            @JoinColumn(name = "LSFCF_CODSOC", referencedColumnName = "LSFOC_CODSOC") }, 
    inverseJoinColumns = { @JoinColumn(name = "LSFCF_CODLSC", referencedColumnName = "LSFOR_CODLSC"),
            @JoinColumn(name = "LSFCF_CODFOR", referencedColumnName = "LSFOR_CODFOR"),
            @JoinColumn(name = "LSFCF_CODSOC", referencedColumnName = "LSFOR_CODSOC") })

before the getter. But it won't work... The error, when trying to access the distant collection is :

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [beans-dao.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Repeated column in mapping for collection: package.LSFOCTB.distantCollection column: LSFCF_CODLSC

Have already managed to make an hibernate mapping work for a ManyToMany relationship ? If so, what is wrong with my mapping ? Thank you for your help !


Asked by: Rebecca891 | Posted: 21-01-2022






Answer 1

The problem seems to be that you are creating a join table with 6 columns and there are duplicate names for your columns. You are actually creating 2 columns with the name LSFCF_CODLSC and 2 columns named LSFCF_CODFOR and 2 columns named LSFCF_CODSOC.

I would suggest that you try this:

@JoinTable(name = "LSFCFTB", joinColumns = {
                    @JoinColumn(name = "LSFOC_LSFCF_CODLSC", referencedColumnName = "LSFOC_CODLSC"),
                    @JoinColumn(name = "LSFOC_LSFCF_CODFOC", referencedColumnName = "LSFOC_CODFOC"),
                    @JoinColumn(name = "LSFOC_LSFCF_CODSOC", referencedColumnName = "LSFOC_CODSOC") }, 
        inverseJoinColumns = { @JoinColumn(name = "LSFOR_LSFCF_CODLSC", referencedColumnName = "LSFOR_CODLSC"),
                    @JoinColumn(name = "LSFOR_LSFCF_CODFOR", referencedColumnName = "LSFOR_CODFOR"),
                    @JoinColumn(name = "LSFOR_LSFCF_CODSOC", referencedColumnName = "LSFOR_CODSOC") })

or something similar (according to your naming convention) to give each column a unique name.

Answered by: Owen214 | Posted: 22-02-2022



Similar questions

java - Can add extra field(s) to @ManyToMany Hibernate extra table?

I have these two class(table) @Entity @Table(name = "course") public class Course { @Id @Column(name = "courseid") private String courseId; @Column(name = "coursename") private String courseName; @Column(name = "vahed") private int vahed; @Column(name = "coursedep") private int dep; @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "student_course", joinColum...


java - @ManyToMany with Hibernate and JPA

I have two tables that are bound by a ManyToMany relationship. Table 1 is TimeSlot and has a collection of documents created in that time slot. Table 2 is Documents and has a collection of TimeSlots as a given document could be modified many times in different timeslots. In this particular program for each document I find or create a document row for it. Further I find or create a time slot that represents...


hibernate - @ManyToMany error in java

I have a org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: Forms.CarsRentalOrder.OrderUnits on this code: @Entity public class CarsRentalOrder implements Serializable { @ManyToMany private List<Vehicle> OrderUnits; //methods and variables } wh...


java - Hibernate using of @ManyToMany and update both ways

I have 2 classes, a Room class and a Student class. A Room can have many Students, while a Student also can have many Rooms. Therefore i used @ManyToMany relationship public class Room { @ManyToMany private Collection<Student> studentList = new ArrayList<Student>(); } public class Student { @ManyToMany(mappedBy="studentList") private Collection<Room>...


java - Hibernate 4 - Can add extra field(s) to @ManyToMany Hibernate extra table?

I have same question which was answered in hibernate 3 here. However, I can't apply this to hibernate 4 as there is no @CollectionOfElements annotation. Can someone provide the solution that can be executed using hibernate 4? EDIT: changed @CollectionOfElements to @ElementCollection worked for me. What...


Java - Hibernate @ManyToMany mapping adds records in the database only in one direction

I am new to Hibernate and I encountered a problem. I have two entities (Student and Course). Students can have many courses, and courses can have many students. When I am making a new course and add it to the database and in the same session I add this course to the student's courses (student.getCourses().add(course)) everything is working fine and records are added both in the Courses table and the Students_Courses table....


java - Hibernate @ManyToMany -- join table isn't being populated

I'm trying to implement a @ManyToMany relationship, but my join table isn't being populated. My app models a bunch of Competition objects to Competitor objects. There is a group of Competitors. Within that group, each Competitor is matched with each other Competitor to form a Competition. So each Competition can have a list of Competitor objects. And each Competitor can be in multiple Competitions. Ent...


java - Hibernate mapping @manytomany

I got 3 tables: Bus, Driver and BusDriver and I need to get bus entity with set of drivers. I have already understood how to do this but there is a date field in BusDriver and I need to include it in set. For example I got {bus_id, bus_model, ... {driver1, driver2}} but I need {bus_id, bus_model, ... {{driver1, date}, driver2, date}}


java - Hibernate - automatic @ManyToMany update

I have Article entity, which has related Tags: @Entity @Table(name = "articles") public class Article implements Serializable{ //other things @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) private List<Tag> tags; } And Tag entity, which has related Articles: @Entity @Table(name = "tags") p...


java - @ManyToMany Hibernate only update JoinTable

I have below entities and @ManyToMany mapping between the two. @Entity @Table(name = "user") public class User implements java.io.Serializable { /** * */ private static final long serialVersionUID = 5340562707217344212L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long userId; private String userName; private String password; p...


java - Can add extra field(s) to @ManyToMany Hibernate extra table?

I have these two class(table) @Entity @Table(name = "course") public class Course { @Id @Column(name = "courseid") private String courseId; @Column(name = "coursename") private String courseName; @Column(name = "vahed") private int vahed; @Column(name = "coursedep") private int dep; @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "student_course", joinColum...


java - JPA @ManyToMany on only one side?

I am trying to refresh the @ManyToMany relation but it gets cleared instead... My Project class looks like this: @Entity public class Project { ... @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinTable(name = "PROJECT_USER", joinColumns = @JoinColumn(name = "PROJECT_ID", referencedColumnName = "ID"), inverseJoinColumns = @JoinColumn(name = "USER_ID", referenc...


java - @ManyToMany without join table (legacy database)

I have to apply JPA in a legacy database with an awful design. Unfortunately is not possible to change it. Luckily is only for read-only access. One of the strangest things I found is a "many-to-many" relationship without a join (or intermediate) table. This is a simplification of the table structure: USER ACCESS ---- ------ ID int primary key ID int...


java - @Manytomany add extra field with no join Entity

I created a @ManyToMany relation between two table, without have a relational entity through them. Something like this: @Entity public class Category{ @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "CATEGORY_ID", unique = true, nullable = false) private Long categoryId; } @Entity public class Content { @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "CONTENT_ID", unique = true, nullabl...


java - Hibernate : Use of @OneToMany or @ManyToMany targeting an unmapped class

i can't find where is my error, where i'm not mapping my class, but to me everything shoud be fine.. but isn't. Can someone help me to find it ? org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.bytecode.entities.Event.categorytagit[com.bytecode.entities.Categorytagit] at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(Co...


java - How to Fetch @OneToMany and @ManyToMany Entities

my issue is very related to the following: Why am I getting a Hibernate LazyInitializationException in this Spring MVC web application when the data displays correctly? I have the following properties on a particular entity: @OneToMany(fetch = FetchType.E...


java - @ManyToMany with Hibernate and JPA

I have two tables that are bound by a ManyToMany relationship. Table 1 is TimeSlot and has a collection of documents created in that time slot. Table 2 is Documents and has a collection of TimeSlots as a given document could be modified many times in different timeslots. In this particular program for each document I find or create a document row for it. Further I find or create a time slot that represents...


java - @ManyToMany - data does not persist in Database

Simplifying, in my database I have tables: Car (pk="id_car") CarAddon (pk="id_car_fk,id_addon_fk", `FK_car_addon_addon` FOREIGN KEY (`id_addon_fk`) REFERENCES `addon` (`id_addon`) `FK_car_addon_car` FOREIGN KEY (`id_car_fk`) REFERENCES `car` (`id_car`) Addon (pk="id_addon") Shortly: I have cars, many cars can has many addons (like ABS etc). There are tables with cars, addons, and on...


java - How to persist @ManyToMany relation - duplicate entry or detached entity

I want to persist my entity with ManyToMany relation. But i have some problem during persisting process. My entities : @Entity @Table(name = "USER") public class User implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.IDENTITY) Long userId; @Column(name = "NAME", unique = true, nulla...


hibernate - @ManyToMany error in java

I have a org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: Forms.CarsRentalOrder.OrderUnits on this code: @Entity public class CarsRentalOrder implements Serializable { @ManyToMany private List<Vehicle> OrderUnits; //methods and variables } wh...






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top