File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Primary key / Forign Key Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "Primary key / Forign Key" Watch "Primary key / Forign Key" New topic
Author

Primary key / Forign Key

Shariq Roomi
Ranch Hand

Joined: Oct 19, 2004
Posts: 31
I am getting the exception org.hibernate.AnnotationException: A Foreign key refering Teacher from Student has the wrong number of column. should be 2

The code is as follows. Can some one please help me with this code , what am I doing wrong ? :-( I have eliminated get/set from all classes

DB Schema:

Teacher(id,name)
Student(id , teacher_id, teacher_name)

@Entity
@Table(name="Teacher")

public class Teacher implements Serializable {

@EmbeddedId
private PK pk = new PK();

@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name="teacher_id")
private List<Student> student;

public List<Student> getStudent() {
return student;
}

public void setStudent(List<Student> student) {
this.student = student;
}
}


/////////////////////
@Entity
@Table(name="Student")

public class Student implements Serializable {

@Id
@Column(name="id")
private int id;

@ManyToOne
@JoinColumns ( {@JoinColumn(nullable=false, name="id"),
@JoinColumn(nullable=false, name="name")})
private Teacher teacher;
}

///////////////////////////
@Embeddable
public class PK implements Serializable
{
private int id;
private String name;
}
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17225
    
    1

@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name="teacher_id")
private List<Student> student;


Shouldn't that include teacher_name?

I am really wondering why teacher_name is part of your Teacher's Primary Key. The id is unique and should be the Primary Key by itself, you can create a unique key on the combination, but it as the Primary Key is not a good idea.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Shariq Roomi
Ranch Hand

Joined: Oct 19, 2004
Posts: 31
Thanks Mark for your reply. The schema is sample to demonstrate the problem.
I did some research and was able to see the multiple columns for joining which you are telling me.

In teacher class, I did

@OneToMany(cascade = {CascadeType.ALL})
@JoinColumns ( {@JoinColumn(nullable=false, name="teacher_id"),
@JoinColumn(nullable=false, name="teacher_name")})
private List<Student> student;

and in student class, I did

@ManyToOne (fetch=FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinColumns ( {@JoinColumn(nullable=false, name="teacher_id",
updatable=false, insertable=false),
@JoinColumn(nullable=false, name="teacher_name",
updatable=false, insertable=false)})
private Teacher teacher;

But then I got following exception

org.springframework.orm.hibernate3.HibernateSystemException: could not get a field value by reflection getter of PK.id; nested exception is org.hibernate.PropertyAccessException: could not get a field value by reflection getter of PK.id
Caused by: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of PK.id

at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get(DirectPropertyAccessor.java:35)
at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValue(AbstractComponentTuplizer.java:64)
at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValues(AbstractComponentTuplizer.java:70)
at org.hibernate.tuple.component.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:83)
at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:353)
at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:348)
at org.hibernate.event.def.AbstractVisitor.processComponent(AbstractVisitor.java:82)
at org.hibernate.event.def.ReattachVisitor.processComponent(ReattachVisitor.java:43)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:107)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:123)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:268)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:21
7)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java
:93)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
Benito d'Almeida
Greenhorn

Joined: Mar 31, 2011
Posts: 1
In you query you have to use the property name not the object

you should do
....
where teacher.id = :id
 
I agree. Here's the link: http://jrebel.com/download
 
subject: Primary key / Forign Key
 
Similar Threads
How to map ManyToOne relationship with Composite key?
Adding associations to a join entity..
Could not determine type for: java.util.Set
@OneToMany primary key part of foreign key
How to store composite objects in Hibernate