[ORM] Support for upsert semantics
by Gunnar Morling
Hi,
A common requirement that comes up repeatedly is "upsert" operations, i.e.
either insert a record or update the existing one with the same PK, if it
already exists. Many (most?) RDBMS support it, either by implementing SQL
2003's MERGE keyword or via proprietary alternatives [1].
I think it'd be great if there was something like Session#upsert(),
exposing these semantics at the entity level. Perhaps the idea was already
brought up in the past, but I wanted to throw it out in any case.
Any thoughts?
Cheers,
--Gunnar
[1]
https://vladmihalcea.com/how-do-upsert-and-merge-work-in-oracle-sql-serve...
6 years, 3 months
@OneToOne with @PrimaryKeyJoinColumn(s) vs @MapsId without value element
by Gail Badner
The fix for HHH-12436 involves correcting the foreign key direction for
"real" one-to-one associations. I've been looking into the ramifications of
this change because I'm concerned that applications can rely on the old
(incorrect) foreign key direction.
In the process I've found that Hibernate treats:
@OneToOne
@PrimaryKeyJoinColumn
private Employee employee;
differently from:
@OneToOne
@MapsId
private Employee employee;
I believe they should be treated consistently. You can see my reasoning
below. [1]
Before going into details about how they are treated differently, I'd like
to get confirmation, in case I am missing some subtlety.
Could someone please confirm this?
Regards,
Gail
---------------------------------------------------------------------------------------------
[1]
In 2.4.1.3 Examples of Derived Identities, Example 4(b) uses MapsId without
the value element as follows:
@MapsId
@JoinColumn(name="FK")
@OneToOne Person patient;
This example has the following footnote:
"[15] Note that the use of PrimaryKeyJoinColumn instead of MapsId would
result in the same mapping in this example. Use of MapsId
is preferred for the mapping of derived identities."
The description has a footnote that says that using PrimaryKeyJoinColumn
instead of MapsId would result in the same mapping.
In 11.1.45 PrimaryKeyJoinColumns Annotation, Example 2 uses
@PrimaryKeyJoinColumns as follows:
@OneToOne
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name="ID",
referencedColumnName="EMP_ID"),
@PrimaryKeyJoinColumn(name="NAME",
referencedColumnName="EMP_NAME")})
EmployeeInfo info;
This example has the following footnote:
"[123]Note that the derived identity mechanisms decribed in section 2.4.1.1
is now preferred to the use of PrimaryKeyJoinColumn for
this case."
6 years, 3 months