Hibernate version: 3.2.1
Hi,
I'm new in hibernate and annotations.
I have tried to make a simple app but it doen't work.
I have three tables:
Student(p_id int IDENTITY,
p_first_name nvarchar(50),
p_last_name nvarchar(50))
Course(c_id int IDENTITY,
c_name nvarchar(50))
Student_Course(id int IDENTITY,
p_id int,
c_id)
the p_id and c_id are foriegn keys to Student and Course tables respectively.
The java classes are:
Student class
@Entity
@Table(name = "student", uniqueConstraints = {})
public class Student {
private int id;
private String firstName;
private String lastName;
private Set<Course> courses;
public Student(){
}
/**
* @return the id
*/
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="p_id", unique=false, nullable=true, updatable=true, insertable=true)
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the firstName
*/
@Column(name="p_first_name", unique=false, nullable=true, updatable=true, insertable=true)
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
@Column(name="p_last_name", unique=false, nullable=true, updatable=true, insertable=true)
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* @return the courses
*/
@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, targetEntity = Course.class)
@JoinTable(name = "student_course", joinColumns = { @JoinColumn(name = "p_id") }, inverseJoinColumns = { @JoinColumn(name = "c_id") })
public Set<Course> getCourses() {
return courses;
}
/**
* @param courses the courses to set
*/
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
Course class
package test;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name = "course", uniqueConstraints = {})
public class Course {
private int id;
private String name;
private Set<Student> students;
public Course() {
super();
}
/**
* @return the id
*/
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="c_id", unique=true, nullable=false, insertable=true, updatable=false)
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
@Column(name="c_name", unique=true, nullable=false, insertable=true, updatable=false)
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the students
*/
@ManyToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, targetEntity = Student.class)
@JoinTable(name = "student_course", joinColumns = { @JoinColumn(name = "c_id") }, inverseJoinColumns = { @JoinColumn(name = "p_id") })
public Set<Student> getStudents() {
return students;
}
/**
* @param students the students to set
*/
public void setStudents(Set<Student> students) {
this.students = students;
}
}
Test class
package test;
import java.util.HashSet;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class StudentTest {
public static void main(String[] args){
StudentTest test = new StudentTest();
test.test();
}
public void test(){
try{
SessionFactory factory = new AnnotationConfiguration()
.addAnnotatedClass(Student.class)
.addAnnotatedClass(Course.class)
.configure()
.buildSessionFactory();
Session session = factory.openSession();
Student s = new Student();
s.setFirstName("john");
s.setLastName("bee");
s.setCourses(new HashSet<Course>());
session.save(s);
// session.persist(s);
// session.flush();
session.close();
}catch(HibernateException e){
e.printStackTrace();
System.out.println(e.getCause().toString());
}
}
}
I don't know why, but the instance has not been saved in the database.
Can someone help me?
|