3

Hi below is my entities with manytoone association between them

student.java

@Entity
@Table(name = "student")
public class student{

    @Id
    @Column(name = "UserID")
    private String userid;

    @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumns({
       @JoinColumn(name = "userrole", referencedColumnName = "VALUE"),
       @JoinColumn(name = "userrole", referencedColumnName = "DESCRIPTION")

    })
    private studentdetails userrole;

//setters and getters 
//constructor

}

studentdetails.java

@Data
@Entity
@Table(name = "student_details")
public class studentdetails {

    @Id
    @Column(name = "VALUE")
    private String value;

    @Id
    @Column(name = "DESCRIPTION")
    private String description;

   //setters and getters 
   //constructor
}

appmain.java

public static void main()
{

//session configuration

studentdetails  sd = new studentdetails();
sd.setvalue("abc");
sd.setdescription("abcdef");

student student1 = new student();
student.setuserid("1");
student.userrole(sd);

student student2 = new student();
student.setuserid("2");
student.userrole(sd);

session.save(student1 );
session.save(student2 );


}

below are the columns in my 2 table

student: 

UserID
userrole

student_details:

VALUE
DESCRIPTION

the "value" in "student_details" should enter into "userrole" of student table

but when I execute my appmain I am getting below error

org.hibernate.MappingException: Foreign key (FK6D56043A4415BDB5:student [userrole])) must have same number of columns as the referenced primary key (student_details [VALUE,DESCRIPTION])
    at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:113)
    at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:96)
    at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1354)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1261)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:383)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1377)
    at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:954)

I tried to solve this but it showing same error please suggest me how to solve this

user3824049
  • 155
  • 2
  • 4
  • 16
  • Is this the complete code of your entities? where is the mapping annotations like `@OneToMany` or `@ManyToOne`? Also logically speaking a Student can have single Student Details mapping and vice versa, so the mapping should be `@oneToOne` right? – Chaitanya Nov 04 '14 at 06:49
  • please find the updated post(added manytoone annotation), I have missed to add it in the post – user3824049 Nov 04 '14 at 06:52

1 Answers1

7

To fix the issue, change your code like this:

 @ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
    @JoinColumns({
       @JoinColumn(name = "userrole_value", referencedColumnName = "VALUE"),
       @JoinColumn(name = "userrole_desc", referencedColumnName = "DESCRIPTION")

    })
    private studentdetails userrole;

Reason for the issue: In mapping:

@JoinColumns({
       @JoinColumn(name = "userrole", referencedColumnName = "VALUE"),
       @JoinColumn(name = "userrole_desc", referencedColumnName = "DESCRIPTION")

    })

You are telling hibernate to create a foreign key in Student entity table called userrole that refers to the column called VALUE in StudentDetails entity table.

Then in next line you are again telling hibernate to use the same colum name - userrole as FKey for the column DESCRIPTION in StudentDetails, so this line overrides the previous one.

So hibernate sees that you are trying to have a single column as foreign key in Student entity table that maps to StudentDetails entity table. But the StudentDetails table has composite key made of 2 columns so hibernate throws an exception.

org.hibernate.MappingException: Foreign key (FK6D56043A4415BDB5:student [userrole])) must have same number of columns as the referenced primary key (student_details [VALUE,DESCRIPTION])

Additional information:

You are trying to declare a composite Id for the entity StudentDetails, so this entity should implelemt Serializable interface and this is mandatory. Now this composite Id class should override equals() and hashcode() methods.

Just a suggestion, try to follow Java naming conventions for your entities and fields.

Chaitanya
  • 15,403
  • 35
  • 96
  • 137
  • what are userrole_value and userrole_desc columns, we do not have such columns in our table – user3824049 Nov 04 '14 at 07:18
  • @user3824049, ok, then update your question and show how your columns look like. You need to tell hibernate what are the foreign keys in Student table. As per your question you are creating a single column called `userrole` and telling hibernate to map it to 2 columns of student_details table which is not correct. – Chaitanya Nov 04 '14 at 07:20
  • You have a composite key for StudentDetails with fields `value` and `description` then you cannot have a single column in Student table to have a mapping with StudentDetails. This is not a restriction from Hibernate side, it is a DB restriction. So make sure if `description` is really part of `PrimaryKey` of StudentDetails, if yes then the column `userrole` is just another column of Student table, it cannot be called as FKey, you cannot map this with Hibernate. – Chaitanya Nov 04 '14 at 07:30
  • need to insert values of "value" column in child table to userrole column of parent table. First I have tried @JoinColumn(name = "userrole ", referencedColumnName = "VALUE") also which is giving another error as "entity type not mapped to a single property", so I tried with joincolumns{} syntax – user3824049 Nov 04 '14 at 07:30
  • Make sure if description is really part of PrimaryKey of StudentDetails, if yes then the column `userrole` is just another column of Student table, it cannot be called as FKey, you cannot map this with Hibernate. – Chaitanya Nov 04 '14 at 07:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64217/discussion-between-user3824049-and-chaitanya). – user3824049 Nov 04 '14 at 07:32