I am having problems with one to many relationship I was surfing from web and could not fix. I double check @Entity declarations they were referring to import javax.persistence.Entity;
before that I tried unidirectional only ManyToOne and it was working
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
@OneToMany(targetEntity = Book.class, mappedBy = "student", cascade = CascadeType.ALL)
private Set<Book> books = new HashSet<>();
public Student() {
}
// accessors ...
}
and Book object
@Entity
@Table(name = "book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;
private String name;
@ManyToOne
@JoinColumn(name = "student_id")
private Student student;
public Book() {
}
//// accessors
}
and here is my hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/***?useSSL=false</property>
<property name="connection.username">***</property>
<property name="connection.password">***</property>
<property name="connection.pool_size">2</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
</session-factory>