gpt4 book ai didi

java - Hibernate @OneToMany 不保存外键

转载 作者:行者123 更新时间:2023-12-02 00:51:14 25 4
gpt4 key购买 nike

我在保存一对多关系时遇到问题。看起来它是将列表的元素保存在表中,但没有将外键保存在正确的表中。

@Entity
@Table(name = "tbl_applications")
public class ApplicationEntity
{
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "intAPI", referencedColumnName = "intCode")
private ApiResourceEntity objApi;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "strApplication")
List<ApplicationRoleEntity> colApplicationRoles = new ArrayList<ApplicationRoleEntity>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "strApplication")
List<URLEntity> colUrls = new ArrayList<URLEntity>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "strApplication")
List<KeyCredentialEntity> colKeyCredentials = new ArrayList<KeyCredentialEntity>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "strApplication")
List<PasswordCredentialEntity> colPasswordCredentials = new ArrayList<PasswordCredentialEntity>();
@OneToMany(cascade = CascadeType.ALL, mappedBy = "strApplication")
List<RequiredResourceAccessEntity> colRequiredResourceAccess = new ArrayList<RequiredResourceAccessEntity>();
}
@Entity
@Table(name = "tbl_apiresources")
public class ApiResourceEntity
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "intCode")
private Integer intCode;
private Integer intRequestedAccessTokenVersion;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "intCode")
private List<PermissionScopeEntity> colOauth2PermissionsScope = new ArrayList<PermissionScopeEntity>();
}
@Entity
@Table(name = "tbl_permissionsscope")
public class PermissionScopeEntity
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "intCode")
private Integer intCode;
private String strAdminConsentDescription;
private String strAdminConsentDisplayName;
private String strID;
private Boolean bolIsEnabled;
private String strOrigin;
private String strType;
private String strUserConsentDescription;
private String strUserConsentDisplayName;
private String strValue;
}
<小时/>

它在数据库中保存的内容的示例:

tbl_applications
intAPI = 1
tbl_apiresources
intCode = 1
intRequestAccessTokenVersion = 2
intOauth2PermissionScopes = NULL
tbl_permissionsscope
intCode = 1
strAdminConsentDescription = xxxxxx
strAdminConsentDisplayName = xxxxxx
strID = xxxxxx
bolIsEnabled = xxxxxx
strOrigin = xxxxxx
strType = xxxxxx
strUserConsentDescription = xxxxxx
strUserConsentDisplayName = xxxxxx
strValue = xxxxxx

它保存列表的元素(权限范围),但是外键不保存在表 tbl_apiresources 上。也许是因为标识符是一个身份字段(自行生成)

ApplicationEntity 对象的所有 @OneToMany 关系均已正确保存。 @OneToMany 无法指向 @ManyToOne,因为我收到循环引用异常

最佳答案

当您仅指定@OneToMany时关系,即从父级到子级的单向关系,hibernate将通过连接表实现映射。当您保存父/子关系时,键将插入到连接表中:

@Entity
public class Parent {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@OneToMany
Set<Child> children;
}
@Entity
public class Child {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
}

连接表parent_child中的结果

Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: create table child (id bigint not null, primary key (id))
Hibernate: create table parent (id bigint not null, primary key (id))
Hibernate: create table parent_children (parent_id bigint not null, children_id bigint not null, primary key (parent_id, children_id))
Hibernate: alter table parent_children add constraint UK_fh9rqlaf2416b31ec7n92nrfh unique (children_id)
Hibernate: alter table parent_children add constraint FK2li53iimvay1c1bjvc1hed3gl foreign key (children_id) references child
Hibernate: alter table parent_children add constraint FKdnxvj4hlnv40nix37bpjsvecn foreign key (parent_id) references parent
Hibernate: call next value for hibernate_sequence
Hibernate: insert into child (id) values (?)
Hibernate: call next value for hibernate_sequence
Hibernate: insert into parent (id) values (?)
Hibernate: insert into parent_children (parent_id, children_id) values (?, ?)

如果您希望 FK 位于子表中(您确实这样做了),则定义从子表到父表的单向映射。

@Entity
public class Parent {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
}
@Entity
public class Child {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@ManyToOne
Parent parent;
}

这不会创建连接表

Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: create table child (id bigint not null, parent_id bigint, primary key (id))
Hibernate: create table parent (id bigint not null, primary key (id))
Hibernate: alter table child add constraint FK7dag1cncltpyhoc2mbwka356h foreign key (parent_id) references parent
Hibernate: call next value for hibernate_sequence
Hibernate: insert into parent (id) values (?)
Hibernate: call next value for hibernate_sequence
Hibernate: insert into child (parent_id, id) values (?, ?)

如果您想要双向映射,则添加两者,但了解您应该像第二个示例一样进行持久化并使用父 Set<Child> children仅用于出于性能原因的查询。

@Entity
public class Parent {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@OneToMany
Set<Child> children;
}
@Entity
public class Child {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

@ManyToOne
Parent parent;
}

插入后,再用left outer join fetch查询或者更好地使用 EntityGraph .

"from Parent p left outer join fetch p.children where p.id = :id"

关于java - Hibernate @OneToMany 不保存外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57857026/

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