Auditing of collections of embeddables only supporting single element changes

Description

I’ve started to move from our local 3.6.7 branch (https://github.com/kristofferlundberg/hibernate-core/tree/3.6.7.Final_HHH-6613), where support for auditing of collections of embeddables/components is implemented, to Hibernate 4.2.0.Final.

Sadly it looks like there are some issues regarding the merge.
The unit tests that I created didn’t cover adding/removing multiple items in a set at once.
If I had added better coverage in those tests, it would have shown the underlying problem and the reason for why I added the SETORDINAL.

The secondary table doesn't contain the columns of the embeddable in the primary key since they may be null.
Not all database vendors allow nulls in the primary key/unique indexes (Microsoft SQL Server in our case), so we need some other kind of value distinguishing the different rows.
That’s why I added the SETORDINAL, being the ordinal of the element in the change set.

The current implementation in 4.2.0.Final generates:
CREATE TABLE embsetent_set_aud
(
rev INTEGER NOT NULL,
revtype TINYINT NOT NULL,
embeddablesetentity_id INTEGER NOT NULL,
str1 VARCHAR(255),
audcomp_description VARCHAR(255),
audcomp_key VARCHAR(255),
audcomp_value VARCHAR(255),
PRIMARY KEY (rev, revtype, embeddablesetentity_id)
)

This only allows for one entry per revision type and owning entity for each set.

When adding the SETORDINAL:

CREATE TABLE embsetent_set_aud
(
rev INTEGER NOT NULL,
revtype TINYINT NOT NULL,
embeddablesetentity_id INTEGER NOT NULL,
setordinal INTEGER NOT NULL,
str1 VARCHAR(255),
audcomp_description VARCHAR(255),
audcomp_key VARCHAR(255),
audcomp_value VARCHAR(255),
PRIMARY KEY (rev, revtype, embeddablesetentity_id, setordinal)
)

I’ve created a branch based on the 4.2.0.Final tag and added a suggested solution:

https://github.com/kristofferlundberg/hibernate-core/commit/fb051813ad7f4aa66bfb96886c09bd528832cdcb

Environment

Hibernate: 4.2.0.Final
DB: any

Activity

Show:
Kristoffer Lundberg
April 11, 2013, 6:23 PM

I've added the requested changes to the test and some additional scenarios.
The changes have been formatted with the formatting rules you supplied and documentation has been added.
I also removed the configurability of the column type, since the code only supported Integer.

4.2: https://github.com/hibernate/hibernate-orm/pull/505
master: https://github.com/hibernate/hibernate-orm/pull/506

Lukasz Antoniak
April 14, 2013, 10:07 PM

Thanks for registering the issue and developing fix quickly.

ArónP
June 4, 2015, 7:57 AM

I want to use @ElementCollection on a set of type List, like this:

@ElementCollection
@CollectionTable(name = "MY_PRODUCT", joinColumns = @JoinColumn(name = "ID_PRODUCT"))
private List<MY_PRODUCT> products = new ArrayList<>();

After that, I see Hibernate generates the following script sql:

CREATE TABLE HIST_MY_PRODUCT (
REV number(10,0) not null,
REVTYPE number(3,0) not null,
ID_PRODUCT number(19,0) not null,
PRICE number(10,0),
primary key (REV, REVTYPE, ID_PRODUCT)
);

And if I use a set of type Set, like this:

@ElementCollection
@CollectionTable(name = "MY_PRODUCT", joinColumns = @JoinColumn(name = "ID_PRODUCT"))
private Set<MY_PRODUCT> products = new LinkedHashSet<>();

Hibernate generates the following script sql:

CREATE TABLE HIST_MY_PRODUCT (
REV number(10,0) not null,
REVTYPE number(3,0) not null,
ID_PRODUCT number(19,0) not null,
SETORDINAL number(10,0) not null,
PRICE number(10,0),
primary key (REV, REVTYPE, ID_PRODUCT, SETORDINAL)
);

So, I see the aditional field "SETORDINAL" only for the set of type Set and not for a type List.

Could you explain this case ? I would not change all my List fields to Set.

Thanks in advance for your response.

Oliver Blume
October 18, 2016, 12:34 PM

I'm facing the same problem as Aron.

Could someone please give an answer whether or not element collections of type List are supported or not?

Oliver Blume
October 18, 2016, 1:00 PM

Found a solution: Adding @OrderColumn seems to work.

Fixed

Assignee

Lukasz Antoniak

Reporter

Kristoffer Lundberg

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure