我已经做了好几个礼拜了。我想链接号码在用户(一个用户对多个数字)和用户的号码(许多号码到一个用户)。我运气不好,需要你的知识。无论我做什么,我总是在这或那样的错误。直截了当地回答该做什么就行了。
application.properties:
spring.jpa.hibernate.ddl-auto=validate用户实体:
@Entity
@Table(name = "user")
public class Users implements Serializable {
private static final long serialVersionUID = 2323232323L;
@Id
@Column(nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
private List<Number> number;编号实体:
@Entity
@Table(name = "number")
public class Number implements Serializable {
private static final long serialVersionUID = 1212121212L;
@Id
@Column(nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(cascade = CascadeType.ALL)
private Users user;液基:
<createTable tableName="user">
<column name="id" type="BIGINT(8)" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="fk_number" type="BIGINT"/>
</createTable>
<createTable tableName="number">
<column name="id" type="BIGINT(8)" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="user" type="BIGINT"/>
</createTable>发布于 2018-08-29 07:10:14
有两个问题您需要解决:
number表上的外键列,而不需要user表上的外键列。@JoinColumn,Hibernate希望外键列的名称遵循此模式<name of the attribute that owns association>_<name of the primary key of referenced entity>。在您的示例中,Hibernate期望number表中有一个number列。您可以在我的终极指南-使用JPA和Hibernate的关联映射中了解有关关联映射的更多信息。如果您保留实体映射并使用以下表定义,它应该可以工作:
<createTable tableName="user">
<column name="id" type="BIGINT(8)" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
</createTable>
<createTable tableName="number">
<column name="id" type="BIGINT(8)" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="user_id" type="BIGINT"/>
</createTable>发布于 2018-08-29 07:10:40
根据我的理解,您将在号码表中拥有一个外键,该外键将表示将与其相关联的用户。
由于您没有提到@JoinColumn (包含外键信息的注释),同时在数字实体中指定@ManyToOne关系,因此,在默认情况下,jpa试图在数字表(不可用)中查找名为user_id的列。
只要添加具有适当属性的注释@JoinColumn,它就会工作。
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="<<name of foreign key in number table>>", refrencedColumnName = "id")refrencedColumnName告诉父实体中必须引用哪一列。
https://stackoverflow.com/questions/52066348
复制相似问题