Hibernate 两次映射同一列并插入
Hibernate Mapping same Column twice and insert
我想插入一个包含两个文件名 organization_id 和 account_id 的行,它们是从同一个 table(组织 table)引用的。
EntityModel.java
@ManyToOne
@JoinColumn(name = "organization_id")
private OrganizationModel organization;
@ManyToOne
@JoinColumn(name = "account_id")
private OrganizationModel account;
OrganizationModel.java
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "organization_id", unique = true, nullable = false)
private int organizationId;
@OneToMany(mappedBy = "organization")
private Set<EntityModel> organization;
但是我收到以下错误。
Repeated column in mapping for entity: com.party.OrganizationModel column: organization_id (should be mapped with insert="false" update="false")
当我为帐户添加 insert="false"
update="false"
时,错误 gone.But 我需要同时插入帐户和组织。
为 EntityModel 添加主键。
这是对我有用的代码:
@Entity
@Table(name = "EntityModel")
public class EntityModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "entity_model_id", unique = true, nullable = false)
private int entityModelId;
@ManyToOne
@JoinColumn(name = "organization_id")
private OrganizationModel organization;
@ManyToOne
@JoinColumn(name = "account_id")
private OrganizationModel account;
}
@Entity
@Table(name = "OrganizationModel")
public class OrganizationModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "organization_id", unique = true, nullable = false)
private int organizationId;
@OneToMany(mappedBy = "organization")
private Set<EntityModel> organization;
}
我想插入一个包含两个文件名 organization_id 和 account_id 的行,它们是从同一个 table(组织 table)引用的。
EntityModel.java
@ManyToOne
@JoinColumn(name = "organization_id")
private OrganizationModel organization;
@ManyToOne
@JoinColumn(name = "account_id")
private OrganizationModel account;
OrganizationModel.java
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "organization_id", unique = true, nullable = false)
private int organizationId;
@OneToMany(mappedBy = "organization")
private Set<EntityModel> organization;
但是我收到以下错误。
Repeated column in mapping for entity: com.party.OrganizationModel column: organization_id (should be mapped with insert="false" update="false")
当我为帐户添加 insert="false"
update="false"
时,错误 gone.But 我需要同时插入帐户和组织。
为 EntityModel 添加主键。
这是对我有用的代码:
@Entity
@Table(name = "EntityModel")
public class EntityModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "entity_model_id", unique = true, nullable = false)
private int entityModelId;
@ManyToOne
@JoinColumn(name = "organization_id")
private OrganizationModel organization;
@ManyToOne
@JoinColumn(name = "account_id")
private OrganizationModel account;
}
@Entity
@Table(name = "OrganizationModel")
public class OrganizationModel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "organization_id", unique = true, nullable = false)
private int organizationId;
@OneToMany(mappedBy = "organization")
private Set<EntityModel> organization;
}