GeeCON Prague 2014*
The moose likes Object Relational Mapping and the fly likes org.hibernate.AnnotationException: No identifier specified for entity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "org.hibernate.AnnotationException: No identifier specified for entity" Watch "org.hibernate.AnnotationException: No identifier specified for entity" New topic
Author

org.hibernate.AnnotationException: No identifier specified for entity

Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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

Joined: Jun 14, 2011
Posts: 11

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

Joined: Jun 12, 2007
Posts: 14
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

Joined: Mar 04, 2009
Posts: 417
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

Joined: Mar 04, 2009
Posts: 417
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

Joined: Jun 14, 2011
Posts: 11

take a look at this example: http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-many-to-one-using-annotations-1.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: org.hibernate.AnnotationException: No identifier specified for entity