0 Replies Latest reply: Feb 17, 2012 7:02 AM by 917543 RSS

    Class with composite Key,  attribute and relation

    917543
      Hello

      I am implementing the jpa part of my project however I have a quite common relation to set up between 2 tables and I think I am in the wrong way

      I have 3 entities : question - answer and project

      The code below should allow you to understand easierly :

      ----- project

      public class Project implements Serializable{
                
           private static final long serialVersionUID = 6478117913922953539L;
           
           @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
           private Long ProjId;

           @Column(nullable = false)
           private String Name;

           @OneToMany(mappedBy="project",targetEntity=AnswerPK.class)
           private List<Answer> Answers;

      ------- question

      public class Question implements Serializable{
           
           private static final long serialVersionUID = -1231353969246302525L;
           
           @Id @GeneratedValue
           private Long QuestId;

           @Column(nullable = false)
           private String Label;

           @Column(nullable = false)
           private String Type;


      -------- Answer

      public class Answer implements Serializable{
           
           private static final long serialVersionUID = -690621748466361598L;
           
           @EmbeddedId
           private AnswerPK key;
           
           private String result;

      ----------- AnswerPK

      public class AnswerPK implements Serializable { 

           private static final long serialVersionUID = 9172011321649468776L;

           @ManyToOne(optional=false)
           @JoinColumn(name = "ProjId",nullable = false)
           private Project project;
           
           @OneToOne(optional=false)
           @JoinColumn(name = "QuestId",nullable = false)
           private Question question;

      however I got this error

      Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.unmod.Domain.Project.Answers[com.unmod.Domain.AnswerPK]

      so I start thinking that's not the way to do that