Java 休眠@OneToMany不保存外键
我在保存一对多关系时遇到了问题。它似乎正在将列表中的元素保存到表中,但没有将外键保存到适当的表中Java 休眠@OneToMany不保存外键,java,hibernate,jpa,Java,Hibernate,Jpa,我在保存一对多关系时遇到了问题。它似乎正在将列表中的元素保存到表中,但没有将外键保存到适当的表中 @Entity @Table(name = "tbl_applications") public class ApplicationEntity { @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "intAPI", referencedColumnName = "intCode") private ApiRes
@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_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;
}
如果希望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 (?, ?)
如果您想要一个双向映射,那么可以同时添加这两个映射,但要理解,您应该像第二个示例一样执行持久化,并且仅出于性能原因,才可以使用父级设置子级
@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;
}
请在下面找到我对代码所做的修改
@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;
@ManyToOne
private ApiResourceEntity objApi;
}
当我添加cascade=CascadeType.ALL时,我得到了以下错误:
org.hibernate.engine.jdbc.spi.SqlExceptionHelper-列名无效
“objApi_内部代码”
有无mappedBy=“intCode”可能是因为您正在使用私有列表coloauth2permissionscope=new ArrayList()初始化列表代码>哪个fk保存在错误的表中?如果您是指
列表的fk
如何将fk列表保存在列中?mappedBy属性必须指向多通关系mappedBy必须具有相应的多通
映射,它指定了关系所属方的外键列。您对@OneToMany的意思是什么?不能指向@ManyTone因为我得到一个循环引用异常,所以绝对可以创建一个双向关系,请分享您如何尝试实现该功能的代码HI:如果我在父项上使用OneToMany,在子项上使用ManyToOne,则会显示以下异常。java.lang.IllegalStateException:org.hibernate.TransientObject异常:瞬态实例上的对象循环引用-瞬态实例上的循环引用:azure.entities.PermissionScopeEntity该异常与onetomany和manytoone无关,它只意味着事务中有一个实体尚未保存,确保保存所有实体或使用级联保存相关实体,如果您共享导致出现此异常的代码,我可以帮助您
@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;
}
"from Parent p left outer join fetch p.children where p.id = :id"
@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;
@ManyToOne
private ApiResourceEntity objApi;
}
java.lang.IllegalStateException: org.hibernate.TransientObjectException: object circular reference on transient instance - circular reference on transient instance: azure.entities.PermissionScopeEntity