Skip to content

Commit

Permalink
HHH-7138 - Hibernate implements JPA @OneToMany collection versioning …
Browse files Browse the repository at this point in the history
…incorrectly
  • Loading branch information
sebersole committed Mar 2, 2012
1 parent a3b02e4 commit ba6ad4d
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 1 deletion.
Expand Up @@ -165,7 +165,7 @@ public abstract class CollectionBinder {
private AccessType accessType;
private boolean hibernateExtensionMapping;

private String explicitType = "";
private String explicitType;
private Properties explicitTypeParameters = new Properties();

protected Mappings getMappings() {
Expand Down
@@ -0,0 +1,48 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) ${year}, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.jpa.version;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Version;
import java.util.ArrayList;
import java.util.List;

/**
* @author Steve Ebersole
*/
@Entity
public class Customer {
@Id
public Long id;

@OneToMany( fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL )
public List<Order> orders = new ArrayList<Order>();

@Version
public long version;
}
@@ -0,0 +1,83 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) ${year}, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.jpa.version;

import org.hibernate.Session;

import org.junit.Test;

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

import static org.junit.Assert.assertEquals;

/**
* @author Steve Ebersole
*/
@TestForIssue( jiraKey = "HHH-7138" )
public class JpaSpecVersionValueUpdatingTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Customer.class, Order.class };
}

@Test
public void testVersionNotIncrementedOnModificationOfNonOwningCollection() {
Customer customer = new Customer();
customer.id = 1L;

Order order = new Order();
order.id = 1L;

order.customer = customer;
customer.orders.add( order );

Session session = openSession();
session.beginTransaction();
session.save( customer );
session.getTransaction().commit();
session.close();

long initial = customer.version;

session = openSession();
session.beginTransaction();
customer = (Customer) session.get( Customer.class, 1L );
Order order2 = new Order();
order2.id = 2L;
order2.customer = customer;
customer.orders.add( order2 );
session.getTransaction().commit();
session.close();


session = openSession();
session.beginTransaction();
customer = (Customer) session.load( Customer.class, 1L );
assertEquals( initial, customer.version );
session.delete( customer );
session.getTransaction().commit();
session.close();
}
}
47 changes: 47 additions & 0 deletions hibernate-core/src/test/java/org/hibernate/jpa/version/Order.java
@@ -0,0 +1,47 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) ${year}, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.jpa.version;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Version;

/**
* @author Steve Ebersole
*/
@Entity
@Table( name = "T_ORDER" )
public class Order {
@Id
public Long id;

@ManyToOne( cascade = CascadeType.ALL )
public Customer customer;

@Version
public long version;
}

0 comments on commit ba6ad4d

Please sign in to comment.