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

Primary key / Forign Key

 
Shariq Roomi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
 
Shariq Roomi
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In you query you have to use the property name not the object

you should do
....
where teacher.id = :id
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic