2016-12-23 3 views
0

私はウェブからサーフィンしていて、修正する。私は、彼らは私が単方向のみManyToOneを試してみましたが、それはマッピングされていないクラスをターゲットとする@OneToManyまたは@ManyToManyの使用:kg.aladin.jdbc.Student.books [kg.aladin.jdbc.Book]

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

を働いていたし、ブックオブジェクト

@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

、ここでは私の休止状態になる前にimport javax.persistence.Entity;を参照して@Entity宣言を再確認します。 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

エンティティは、登録する必要があります。あなたは、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> 

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

はそれが:-)を助け

関連する問題