@OneToMany with @JoinTable throws org.hibernate.exception.ConstraintViolationException

Description

Project reproducing issue is attached.


Possibly related to: https://hibernate.atlassian.net/browse/HHH-6776


Entities:

persistence.xml

Test case:

Output:

Environment

None

Activity

Show:
Guillaume Smet
August 14, 2019, 1:01 PM

thanks for the report. Is it a regression from 5.4.3.Final or it didn't work before either?

Behrang
August 14, 2019, 1:18 PM

I think it was a false alarm – I had annotated the fields incorrectly. Changing the annotation as follows will make it work as expected:

 

However, Hibernate detects some classes of invalid annotations on startup. For example, if I have a field definition like this:

 

Hibernate throws an exception on startup letting the user know that there’s something wrong with his entity definitions:

It would be nice if Hibernate could warn users when they accidentally place the JoinTable annotation on fields of the joined classes, instead of behaving unexpectedly at runtime.

Maybe we can turn this ticket into a feature request to do just that?

warren.crossing@team.telstra.com
September 6, 2019, 2:44 AM

I’m getting the same ERROR with 5.3.10.Final , 5.4.3.Final & 5.4.4.Final

Can you turn on

hibernate.show_sql=true and see/check the create table -

You shouldn’t have to specify JoinColumns, it should be able to generate non conflicting column names for the join table. In fact they were erroneous and masking a bug (I think) in the defaults.

Can you remove the JoinColumns annotations and you’ll need to drop the table first.

I can see the unique key is getting generated incorrectly for me, as it just contains the columns from the owned side, which is totally wrong as I would expect the unique key (i.e primary key) to be the sum of the foriegn keys.

Could be a regression as someone reported it years ago (https://stackoverflow.com/questions/24196239/jpa-2-0-hibernate-generates-incorrect-join-table-pk-for-onetomany-with-joint)

I don’t know if it’s a problem with the dialect or with the provider.

Also my entities have composite keys with IdClasses.

2019-09-06 12:23:51.379 INFO 17352 — [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.3.10.Final}

2019-09-06 12:23:51.389 INFO 17352 — [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found

2019-09-06 12:23:51.631 INFO 17352 — [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}

2019-09-06 12:23:51.861 INFO 17352 — [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.Oracle12cDialect

2019-09-06 12:23:51.981 INFO 17352 — [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [byte[]] overrides previous : org.hibernate.type.BinaryType@2ffa91dc

2019-09-06 12:23:51.981 INFO 17352 — [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [[B] overrides previous : org.hibernate.type.BinaryType@2ffa91dc

2019-09-06 12:23:51.981 INFO 17352 — [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [Byte[]] overrides previous : org.hibernate.type.WrapperBinaryType@6de610c6

2019-09-06 12:23:51.981 INFO 17352 — [ main] org.hibernate.type.BasicTypeRegistry : HHH000270: Type registration [[Ljava.lang.Byte;] overrides previous : org.hibernate.type.WrapperBinaryType@6de610c6

Hibernate: create table Email_ROSTER_PRINCIPLE_SUMMARY (Email_employeeId varchar2(255 char) not null, Email_sendDate timestamp not null, Email_shiftDate date not null, Email_templateName varchar2(255 char) not null, summaries_EMPLOYEE_ID varchar2(255 char) not null, summaries_SHIFT_DATE date not null)

Hibernate: alter table Email_ROSTER_PRINCIPLE_SUMMARY drop constraint UK_aieacubl3jlowfslhis2bt7qr

Hibernate: alter table Email_ROSTER_PRINCIPLE_SUMMARY add constraint UK_aieacubl3jlowfslhis2bt7qr unique (summaries_EMPLOYEE_ID, summaries_SHIFT_DATE)

Hibernate: alter table Email_ROSTER_PRINCIPLE_SUMMARY add constraint FKrd1sp7taeyu5l81co2gc4b924 foreign key (summaries_EMPLOYEE_ID, summaries_SHIFT_DATE) references ROSTER_PRINCIPLE_SUMMARY

Hibernate: alter table Email_ROSTER_PRINCIPLE_SUMMARY add constraint FK3amduajjmw8ae2ddpe2mvjgyk foreign key (Email_employeeId, Email_sendDate, Email_shiftDate, Email_templateName) references Email

Behrang
September 6, 2019, 3:03 AM

In my case I was wrong – in the first code sample, I had placed @JoinTable on both sides of the association. However, according to the API docs:

Specifies the mapping of associations. It is applied to the owning side of an association.

And according to section 11.1.27 of the specification (emphasis mine):

The JoinTable annotation is used in the mapping of entity associations. A JoinTable annotation is specified on the owning side of the association.

That's why Hibernate was behaving in an unexpected way.

When I fixed the annotations, everything started working as expected.


Some accepted StackOverflow answers are unfortunately wrong and misleading.

Behrang
September 6, 2019, 3:16 AM

Here is an updated test with 2 components added:

And here’s the executed SQL statements:

Assignee

Unassigned

Reporter

Behrang

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Blocker
Configure