Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

org.hibernate.AnnotationException: No identifier specified for entity

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this table:


The QuestionTest class only has the mapping:
@ManyToOne
private Question question;
@ManyToOne
private Test test;

I know that the problem can be solved creating an id field, but I don't want to do it. I want the primary key to be the composite key.
Is it possible? How can I do this?

 
Muhammad Ramahy
Greenhorn
Posts: 11
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using Hibernate you can put @Id on both of attributes
this will make it a composite.However, I don't think that you should map a relation table as an entity.
 
Repala Madhu
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do this in the following way.


public class NameId implements Serializable
{
protected String name;
protected int type;
protected String neName;

//Getters and Setters follows
}


@Entity
@Table(name = "NAME_")
@IdClass(NameId.class)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Name Serializable
{
@Id
protected String name;
@Id
protected int type;

protected String MOType;
@Id
protected String neName;
public Name()
{

}
//Getters and Setters follows
}


 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Muhammad Ramahy wrote:If you're using Hibernate you can put @Id on both of attributes

If I put @Id in both a get this.
org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: questionapp.model.QuestionTest.test in questionapp.model.Test.questionsTests

Muhammad Ramahy wrote:However, I don't think that you should map a relation table as an entity.

What do you do then?
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Repala Madhu wrote:You can do this in the following way.

Thanks for mention @IdClass
I followed this tutorial http://www.ibm.com/developerworks/java/library/os-hibernatejpa/index.html
But it's not working for me. I don't know what I'm missing.

When I try to save


I get this error java.lang.ClassCastException: questionapp.model.QuestionTestPK cannot be cast to java.lang.Integer

 
Muhammad Ramahy
Greenhorn
Posts: 11
Eclipse IDE jQuery Spring
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic