使用@OneToMany或@ManyToMany定位未映射的类:kg.aladin.jdbc.Student.books [kg.aladin.jdbc.Book]

时间:2016-12-23 12:04:31

标签: java hibernate jpa

我遇到了一对多关系的问题我从网上冲浪并无法修复。我仔细检查了@Entity声明,他们指的是import javax.persistence.Entity;之前我尝试过单向的ManyToOne并且它正在工作

@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 ...
}

和Book对象

@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

这是我的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>

1 个答案:

答案 0 :(得分:2)

实体必须注册。您可以在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>

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

我希望它有所帮助: - )