Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
130 additions
and
0 deletions.
There are no files selected for viewing
130 changes: 130 additions & 0 deletions
130
...e/src/test/java/org/hibernate/test/insertordering/InsertOrderingWithCascadeOnPersist.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* 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.insertordering; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import javax.persistence.Access; | ||
import javax.persistence.AccessType; | ||
import javax.persistence.CascadeType; | ||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.ManyToOne; | ||
import javax.persistence.OneToMany; | ||
import javax.persistence.TableGenerator; | ||
|
||
import org.hibernate.cfg.AvailableSettings; | ||
import org.hibernate.cfg.Configuration; | ||
import org.junit.Test; | ||
|
||
import org.hibernate.testing.TestForIssue; | ||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; | ||
|
||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; | ||
|
||
/** | ||
* @author Chris Cranford | ||
*/ | ||
public class InsertOrderingWithCascadeOnPersist extends BaseCoreFunctionalTestCase { | ||
@Override | ||
protected Class<?>[] getAnnotatedClasses() { | ||
return new Class<?>[] { MarketBid.class, MarketBidGroup.class, MarketResult.class }; | ||
} | ||
|
||
@Override | ||
protected void configure(Configuration configuration) { | ||
super.configure( configuration ); | ||
configuration.setProperty( AvailableSettings.ORDER_INSERTS, Boolean.TRUE.toString() ); | ||
configuration.setProperty( AvailableSettings.ORDER_UPDATES, Boolean.TRUE.toString() ); | ||
} | ||
|
||
@Test | ||
@TestForIssue(jiraKey = "HHH-11768") | ||
public void testInsertOrderingAvoidingForeignKeyConstraintViolation() { | ||
Long bidId = doInHibernate( this::sessionFactory, session -> { | ||
// create MarketBid and Group | ||
final MarketBidGroup group = new MarketBidGroup(); | ||
final MarketBid bid = new MarketBid(); | ||
bid.setGroup( group ); | ||
session.persist( bid ); | ||
return bid.getId(); | ||
} ); | ||
|
||
// This block resulted in a Foreign Key ConstraintViolation because the inserts were ordered incorrectly. | ||
doInHibernate( this::sessionFactory, session -> { | ||
// Add marketResult to existing Bid | ||
final MarketBid bid = session.load( MarketBid.class, bidId ); | ||
final MarketResult result = new MarketResult(); | ||
result.setMarketBid( bid ); | ||
session.persist( result ); | ||
// create new MarketBid, Group and Result | ||
final MarketBidGroup newGroup = new MarketBidGroup(); | ||
final MarketBid newBid = new MarketBid(); | ||
newBid.setGroup( newGroup ); | ||
final MarketResult newResult = new MarketResult(); | ||
newResult.setMarketBid( newBid ); | ||
session.persist( newBid ); | ||
session.persist( newResult ); | ||
} ); | ||
} | ||
|
||
@Entity(name = "MarketBid") | ||
@Access(AccessType.FIELD) | ||
public static class MarketBid { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.TABLE, generator = "MarketBid") | ||
@TableGenerator(name = "MarketBid", pkColumnValue = "MarketBid", allocationSize = 10000) | ||
private Long id; | ||
|
||
@ManyToOne(optional = false, cascade = CascadeType.PERSIST) | ||
private MarketBidGroup group; | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setGroup(MarketBidGroup group) { | ||
this.group = group; | ||
} | ||
} | ||
|
||
@Entity(name = "MarketBidGroup") | ||
@Access(AccessType.FIELD) | ||
public static class MarketBidGroup { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.TABLE, generator = "MarketBidGroup") | ||
@TableGenerator(name = "MarketBidGroup", pkColumnValue = "MarketBidGroup", allocationSize = 10000) | ||
private Long id; | ||
|
||
@OneToMany(mappedBy = "group") | ||
private final Set<MarketBid> marketBids = new HashSet<>(); | ||
|
||
public void addMarketBid(MarketBid marketBid) { | ||
this.marketBids.add(marketBid); | ||
} | ||
} | ||
|
||
@Entity(name = "MarketResult") | ||
@Access(AccessType.FIELD) | ||
public static class MarketResult { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.TABLE, generator = "MarketResult") | ||
@TableGenerator(name = "MarketResult", pkColumnValue = "MarketResult", allocationSize = 10000) | ||
private Long id; | ||
|
||
@ManyToOne(optional = false) | ||
private MarketBid marketBid; | ||
|
||
public void setMarketBid(MarketBid marketBid) { | ||
this.marketBid = marketBid; | ||
} | ||
|
||
} | ||
} |