我想在DB上插入一个条目,该条目已填充所有字段并且与另一个表/类具有@ManyToOne的关系。为此,我正在调用entityManager.merge(c1)
,其中我的c1
是一个Classification
实例(按照下面的定义),我可以在日志中看到以下内容:
[DEBUG] ...: c=o.h.SQL, m=logStatement, l=92, msg: insert into plat.classification_system (year, system, id) values (?, ?, ?)
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [1] as [VARCHAR] - [2012]
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [2] as [BIGINT] - [1000001]
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [3] as [VARCHAR] - [main_system]
...实际上是我期望看到的东西是...
[DEBUG] ...: c=o.h.SQL, m=logStatement, l=92, msg: insert into plat.classification_system (year, system, id, classification_type) values (?, ?, ?, ?)
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [1] as [VARCHAR] - [2012]
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [2] as [BIGINT] - [1000001]
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [3] as [VARCHAR] - [main_system]
[TRACE] ...: c=o.h.t.d.s.BasicBinder, m=bind, l=65, msg: binding parameter [4] as [VARCHAR] - [abc]
我有以下课程
/* entity being stored */
@Entity
@Table("classification_system", indexes = {
@Index(name="CLASS_IDX1",columnList="ID"),
@Index(name="CLASS_IDX2",columnList="ID, SYSTEM"),
@Index(name="CLASS_IDX3",columnList="ID, CLASS_TYPE_GUID, SYSTEM")
})
class Classification extends ClassificationAbstract implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="classSeq")
@SequenceGenerator(name="classSeq", sequenceName="CLASS_SEQ", allocationSize=1)
private Long id;
//`enter code here` couple of constructors...
private String system;
public Classification() {
super();
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getSystem() {
return this.system;
}
public void setSystem(String system) {
this.system = system;
}
}
@MappedSuperclass
public abstract class ClassificationAbstract implements Serializable {
private static final long serialVersionUID = 1L;
@Column(nullable = false)
@NotNull
private String year;
@ManyToOne(fetch = FetchType.EAGER, optional = false, targetEntity = ClassificationType.class)
@JoinColumns(value = {
@JoinColumn(name = "YEAR", referencedColumnName = "YEAR", insertable = false, updatable = false),
@JoinColumn(name = "CLASS_TYPE_GUID", referencedColumnName = "GUID", insertable = false, updatable = false)
}) //tried using 'true' and doesn't run
private ClassificationType classificationType;
public ClassificationType getClassificationType() {
return classificationType;
}
public void setClassificationType(ClassificationType classificationType) {
this.classificationType = classificationType;
}
public String getYear() {
return this.year;
}
public void setYear(String year) {
this.year = year;
}
}
/* view CLASS_TYPE */
@Entity
@Table(name="CLASS_TYPE", uniqueConstraints =
@UniqueConstraint(columnNames = {"YEAR", "GUID","SYSTEM_TYPE_GUID"})
)
@Immutable
public class ClassificationType extends Type implements Serializable {
private static final long serialVersionUID = 1L;
private String code;
@Column(name = "YEAR", nullable = false)
private String year;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumns({
@JoinColumn(name = "YEAR", referencedColumnName = "YEAR",
insertable = false, updatable = false, nullable = false),
@JoinColumn(name = "SYSTEM_TYPE_GUID", referencedColumnName = "GUID",
insertable = false, updatable = false, nullable = false)
})
private SystemType systemType;
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
public String getYear() {
return this.year;
}
public void setYear(String year) {
this.year = year;
}
public String getSystemType() {
return this.systemType;
}
public void setSystemType(String systemType) {
this.systemType = systemType;
}
}
/* view SYSTEM_TYPE */
@Entity
@Table(name="SYSTEM_TYPE", uniqueConstraints =
@UniqueConstraint(columnNames = {"YEAR", "GUID"})
)
@Immutable
public class SystemType extends Type implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "YEAR", nullable = false)
private String year;
private String code;
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
public String getYear() {
return this.year;
}
public void setYear(String year) {
this.year = year;
}
}
@MappedSuperclass
public abstract class Type implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(length = 48)
private String guid;
public String getGuid() {
return this.guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
}
...代表以下模型:
create table classification_system (
id number(19, 0) not null,
year varchar2(255 char) not null,
class_type_guid(48 char ) not null,
system varchar2(255 char) not null,
primary key (id)
)
create table class_type (
guid (48 char) not null,
code varchar2(255 char) not null,
year varchar2(255 char) not null,
system_type_guid varchar2(48 char) not null,
primary key (guid)
)
create table system_type (
guid (48 char ) not null,
code varchar2(255 char) not null,
year varchar2(255 char) not null,
primary key (guid)
)
我一直在挖掘许多与@ManyToOne
映射/关系有关的帖子,但至今仍找不到我可以使用的任何内容。
这是使用Hibernate 5.0.9构建的。
有人可以帮我这个忙吗? 感谢对此事的任何帮助。
答案 0 :(得分:0)
归根结底,在熟练的帮助下,我仅以下解决方案(仅)结束了Java指令和注释:
原来的df1
antecedent consequent p
0 [test100] [test1] 1.0
1 [test3] [test1,test100,test4] 1.0
类
从Classification
和year
注释中删除了ClassificationAbstract
属性:
insertable = null, updatable = null
从@MappedSuperclass
public abstract class ClassificationAbstract implements Serializable {
private static final long serialVersionUID = 1L;
@ManyToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumns(value = {
@JoinColumn(name = "YEAR", referencedColumnName = "YEAR", nullable = false),
@JoinColumn(name = "CLASS_TYPE_GUID", referencedColumnName = "GUID", nullable = false)
})
private ClassificationType classificationType;
public ClassificationType getClassificationType() {
return classificationType;
}
public void setClassificationType(ClassificationType classificationType) {
this.classificationType = classificationType;
}
}
和.hashCode()
中删除了.equals()
和ClassificationType
的定义,因为它们使对象层的所有内容变得混乱。
保留SystemType
和.hashCode()
中的.equals()
和ClassificationTypePK
定义。 https://vladmihalcea.com/the-best-way-to-map-a-composite-primary-key-with-jpa-and-hibernate/
现在一切正常,甚至JPA / HBMDDL生成都在构建预期的列定义:
SystemTypePK
应该记得我在Java EE7 / Java 8应用程序上使用的是Hibernate 5.0.9(最终版),并且已经针对Oracle XE数据库进行了测试,没有任何其他问题。
希望这对遇到相同问题的人很有帮助。