关于hibernate一个很简单的manytoone的问题 看不出错在哪 能帮个忙么
查了下资料
很多人说是@Entity没加 可我加了啊
package com.model;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Classes {
private int id;
private List<Student> student = new ArrayList<Student>();
@OneToMany(mappedBy = "clas", cascade = { CascadeType.ALL })
public List<Student> getStudent() {
return student;
}
public void setStudent(List<Student> student) {
this.student = student;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
package com.model;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Student {
private int id;
private String name;
private String age;
private Classes clas;
@Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@ManyToOne(cascade = { CascadeType.ALL })
public Classes getClas() {
return clas;
}
public void setClas(Classes clas) {
this.clas = clas;
}
}
package com;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import com.model.Classes;
import com.model.Student;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Student s= new Student();
Classes c = new Classes();
s.setClas(c);
System.out.println(s.getId()+"1");
// TODO Auto-generated method stub
SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(s);
session.save(c);
System.out.println(s.getId()+"2");
session.getTransaction().commit();
System.out.println(s.getId()+"4");
}
}
二月 12, 2013 10:49:20 下午 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
二月 12, 2013 10:49:20 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.4.Final}
二月 12, 2013 10:49:20 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
二月 12, 2013 10:49:20 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
二月 12, 2013 10:49:20 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
二月 12, 2013 10:49:20 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
二月 12, 2013 10:49:21 下午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Exception in thread "main" org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.model.Student.clas references an unknown entity: com.model.Classes
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:109)
at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1519)
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1444)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1362)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1731)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
at com.Main.main(Main.java:25)