0

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
}

enter image description here

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>

M.T
  • 881
  • 2
  • 10
  • 17

1 Answers1

2

The Entities have to be registered. You can do it in your xml-File:

<!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>

    <!-- Register Classes -->
    <mapping class="kg.aladin.jdbc.Student"/>
    <mapping class="kg.aladin.jdbc.Book"/>

    <!-- Or Register Packages -->
    <mapping package="kg.aladin.jdbc"/>

</session-factory>

See also:
https://docs.jboss.org/hibernate/stable/annotations/reference/en/html/ch01.html and
Hibernate problem - "Use of @OneToMany or @ManyToMany targeting an unmapped class"

I hope it helps :-)

Community
  • 1
  • 1
Norbert Koch
  • 533
  • 6
  • 17