Я хочу вставить запись в БД, которая заполнит все поля и имеет отношение @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.
Может кто-нибудь помочь мне с этим?
Ценю любую помощь по этому вопросу.