Skip to content

HHH-15045 + HHH-15235 #5331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ https://hibernate.atlassian.net/projects/HHH/versions/31844
* [HHH-14257] - An Entity A with a map collection having as index an Embeddable with a an association to the Entity A fails with a NPE
* [HHH-14251] - Invalid SQL for @Embedded UPDATE
* [HHH-14249] - MultiLineImport fails when script contains blank spaces or tabs at the end of the last sql statement

* [HHH-14216] - Second-level cache doesn't support @OneToOne
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this being changed? We don't typically change existing release notes

Copy link
Member Author

@dreab8 dreab8 Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the PR has been applied to 5.5.0.Alpha1 but we forgot to close the related Jira so I closed it and amended the release notes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, thanks!


Changes in 5.4.14.Final (April 6, 2020)
------------------------------------------------------------------------------------------------------------------------
Expand Down
46 changes: 14 additions & 32 deletions hibernate-core/src/main/java/org/hibernate/type/OneToOneType.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,12 @@ public boolean isOneToOne() {

@Override
public boolean isDirty(Object old, Object current, SharedSessionContractImplementor session) {
if ( isSame( old, current ) ) {
return false;
}

Object oldid = getIdentifier( old, session );
Object newid = getIdentifier( current, session );

return getIdentifierType( session ).isDirty( oldid, newid, session );
return false;
}

@Override
public boolean isDirty(Object old, Object current, boolean[] checkable, SharedSessionContractImplementor session) {
return isDirty(old, current, session);
return false;
}

@Override
Expand Down Expand Up @@ -203,36 +196,25 @@ public boolean useLHSPrimaryKey() {

@Override
public Serializable disassemble(Object value, SharedSessionContractImplementor session, Object owner) throws HibernateException {
if (value == null) {
return null;
}

Object id = ForeignKeys.getEntityIdentifierIfNotUnsaved( getAssociatedEntityName(), value, session );

if ( id == null ) {
throw new AssertionFailure(
"cannot cache a reference to an object with a null id: " +
getAssociatedEntityName()
);
}

return getIdentifierType( session ).disassemble( id, session, owner );
return null;
}

@Override
public Object assemble(Serializable oid, SharedSessionContractImplementor session, Object owner) throws HibernateException {
//the owner of the association is not the owner of the id
Serializable id = ( Serializable ) getIdentifierType( session ).assemble( oid, session, null );

if ( id == null ) {
return null;
}

return resolveIdentifier( id, session );
//this should be a call to resolve(), not resolveIdentifier(),
//because it might be a property-ref, and we did not cache the
//referenced value
return resolve( session.getContextEntityIdentifier(owner), session, owner );
}

/**
* We don't need to dirty check one-to-one because of how
* assemble/disassemble is implemented and because a one-to-one
* association is never dirty
*/
@Override
public boolean isAlwaysDirtyChecked() {
return true;
//TODO: this is kinda inconsistent with CollectionType
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.annotations.onetoone;

import java.io.Serializable;

import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;

import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.junit.Test;

@TestForIssue(jiraKey = "HHH-15235")
public class EmbeddedIdTest extends BaseCoreFunctionalTestCase {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Bar.class, Foo.class };
}

@Test
public void testMerge() {
inTransaction(
session -> {
FooId fooId = new FooId();
fooId.id = "foo";
Foo foo = new Foo();
foo.id = fooId;
Bar bar = new Bar();
BarId barId = new BarId();
barId.id = 1l;
bar.id = barId;
foo.bar = bar;
bar.foo = foo;
session.merge( foo );
}
);
}

@Embeddable
public static class BarId implements Serializable {
private Long id;
}

@Embeddable
public static class FooId implements Serializable {
private String id;
}

@Entity(name = "Bar")
@Table(name = "BAR_TABLE")
public static class Bar {
@EmbeddedId
private BarId id;

private String name;

@OneToOne(mappedBy = "bar")
private Foo foo;
}

@Entity(name = "Foo")
@Table(name = "FOO_TABLE")
public static class Foo {
@EmbeddedId
private FooId id;

private String name;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "bar_id")
private Bar bar;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.hibernate.cfg.Configuration;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.stat.spi.StatisticsImplementor;

import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;

Expand Down Expand Up @@ -109,6 +111,7 @@ private <TPerson extends Person> List<TPerson> getPersons(Class<TPerson> personC
}

@Test
@FailureExpected( jiraKey = "HHH-14216", message = "The changes introduces by HHH-14216 have been reverted see https://github.com/hibernate/hibernate-orm/pull/5061 discussion")
public void OneToOneCacheByForeignKey() throws Exception {
OneToOneTest(PersonByFK.class, DetailsByFK.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.onetoone.flush;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Version;

import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;

import org.hibernate.testing.TestForIssue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.hibernate.testing.orm.transaction.TransactionUtil.inTransaction;
import static org.junit.Assert.assertEquals;

@TestForIssue(jiraKey = "HHH-15045")
public class DirtyFlushTest extends BaseEntityManagerFunctionalTestCase {

@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { User.class, Profile.class };
}

@Before
public void setUp() {
inTransaction( getOrCreateEntityManager(), em -> {
final User user = new User();
user.id = 1;
user.version = 1;

final Profile profile = new Profile();
profile.id = 1;
profile.version = 1;

em.persist( user );
em.persist( profile );
} );
}

@Test
public void testDirtyFlushNotHappened() {
inTransaction( getOrCreateEntityManager(), em -> {
final User user = em.find( User.class, 1 );
assertEquals( 1, user.version );

final Profile profile = em.find( Profile.class, 1 );
assertEquals( 1, profile.version );

profile.user = user;
user.profile = profile;

em.persist( profile );
} );

inTransaction( getOrCreateEntityManager(), em -> {
final Profile profile = em.find( Profile.class, 1 );
assertEquals( 2, profile.version );

final User user = em.find( User.class, 1 );
assertEquals(
"without fixing, the version will be bumped due to erroneous dirty flushing",
1,
user.version
);
} );
}

@After
public void tearDown() {
inTransaction( getOrCreateEntityManager(), em -> {
em.createQuery( "delete from Profile" ).executeUpdate();
em.createQuery( "delete from User" ).executeUpdate();
} );
}

@Entity(name = "User")
@Table(name = "USER_TABLE")
public static class User {
@Id
int id;

@Version
int version;

@OneToOne(mappedBy = "user")
Profile profile;
}

@Entity(name = "Profile")
@Table(name = "PROFILE_TABLE")
public static class Profile {
@Id
int id;

@Version
int version;
@OneToOne // internally Hibernate will use `@ManyToOne` for this field
User user;
}
}