Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.engine.spi.ValueInclusion;
import org.hibernate.id.Assigned;
import org.hibernate.id.ForeignGenerator;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.PostInsertIdentifierGenerator;
import org.hibernate.id.PostInsertIdentityPersister;
Expand Down Expand Up @@ -4731,14 +4733,18 @@ public Boolean isTransient(Object entity, SharedSessionContractImplementor sessi
.getUnsavedValue().isUnsaved( version );
if ( isUnsaved != null ) {
if ( isUnsaved ) {
final Boolean unsaved = entityMetamodel.getIdentifierProperty()
final IdentifierGenerator identifierGenerator = getIdentifierGenerator();
if ( identifierGenerator != null && !( identifierGenerator instanceof ForeignGenerator )
&& !( identifierGenerator instanceof Assigned ) ) {
final Boolean unsaved = entityMetamodel.getIdentifierProperty()
.getUnsavedValue().isUnsaved( id );
if ( unsaved != null && !unsaved ) {
throw new PropertyValueException(
"Detached entity with generated id '" + id + "' has an uninitialized version value '" + version + "'",
getEntityName(),
getVersionColumnName()
);
if ( unsaved != null && !unsaved ) {
throw new PropertyValueException(
"Detached entity with generated id '" + id + "' has an uninitialized version value '" + version + "'",
getEntityName(),
getVersionColumnName()
);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.hibernate.test.version;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.MapsId;
import javax.persistence.OneToOne;
import javax.persistence.Version;

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

@TestForIssue(jiraKey = "HHH-17380")
public class EntityWithNullVersionAndMapsIdTest extends BaseCoreFunctionalTestCase {

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

@Test
public void testPersistEntityWithMapsId() {
Address address = new Address( "Alex" );
inTransaction(
session -> {
session.persist( address );
Person person = new Person( address );
session.persist( person );
}
);
}

@Entity(name = "Person")
public static class Person {

@Id
private String name;

@Version
private Long version;

private String description;

@MapsId
@OneToOne
@JoinColumn(name = "name")
private Address address;

public Person() {
}

public Person(Address address) {
this.name = address.getName();
this.address = address;
}

public String getName() {
return name;
}

public Address getAddress() {
return address;
}
}

@Entity(name = "Address")
public static class Address {
@Id
private String name;

@Version
private Long version;

private String description;

public Address() {
}

public Address(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<!--
~ 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>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.version">

<class name="NewEntityWithNullVersionTest$UserAttributes" table="user_attributes">
<id name="id" type="java.lang.Long" unsaved-value="0"/>
<version name="version" type="java.lang.Long" access="field"/>
<property name="name"/>
</class>

</hibernate-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.hibernate.test.version;

import java.io.Serializable;

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


@TestForIssue(jiraKey = "HHH-17380")
public class NewEntityWithNullVersionTest extends BaseCoreFunctionalTestCase {

@Override
protected String[] getMappings() {
return new String[] { "NewEntityWithNullVersion.hbm.xml" };
}

@Override
protected String getBaseForMappings() {
return super.getBaseForMappings() + "version/";
}

@Test
public void testMergeDetachedEntityWithIdentityId() {
UserAttributes item = new UserAttributes();
item.id = 123L;
item.name = "Abc";
inTransaction(
session -> {
session.saveOrUpdate( item );
}
);
}

public static class UserAttributes implements Serializable {

private Long id = 0L;

private Long version;

private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getVersion() {
return version;
}

public void setVersion(Long version) {
this.version = version;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
}