gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:09:14 26 4
gpt4 key购买 nike

我在网上冲浪时遇到了一对多关系的问题,并且无法修复。我仔细检查了他们引用的 @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>

最佳答案

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

希望对您有帮助:-)

关于java - 使用 @OneToMany 或 @ManyToMany 定位未映射的类 : kg. aladin.jdbc.Student.books[kg.aladin.jdbc.Book],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41301170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com