HHH-3218 : ManyToOne Persisting Cascade in Embeddable -- added test case
This commit is contained in:
parent
199f3e300c
commit
05b815c5f5
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, 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.test.annotations.cascade;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.testing.FailureExpected;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
|
||||
public class CascadeToElementCollectionEmbeddedManyToOneTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
@Test
|
||||
@FailureExpected( jiraKey = "HHH-3218" )
|
||||
public void testPersistCascade() {
|
||||
Session sess = openSession();
|
||||
try {
|
||||
final Transaction trx = sess.beginTransaction();
|
||||
try {
|
||||
final Set<PersonPair> setOfPairs = new HashSet<PersonPair>();
|
||||
setOfPairs.add(new PersonPair(new Person("PERSON NAME 1"), new Person("PERSON NAME 2")));
|
||||
sess.saveOrUpdate( new CodedPairHolder( "CODE", setOfPairs ) );
|
||||
sess.flush();
|
||||
} finally {
|
||||
trx.rollback();
|
||||
}
|
||||
} finally {
|
||||
sess.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[]{
|
||||
CodedPairHolder.class,
|
||||
Person.class,
|
||||
PersonPair.class
|
||||
};
|
||||
}
|
||||
}
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, 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.test.annotations.cascade;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.ElementCollection;
|
||||
import org.hibernate.annotations.ForeignKey;
|
||||
|
||||
@Entity
|
||||
@Table(name = "CODED_PAIR_HOLDER")
|
||||
class CodedPairHolder implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3757670856634990003L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "ID")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "CODE", nullable = false, unique = true, updatable = false, length = 256)
|
||||
private String code;
|
||||
|
||||
@ElementCollection
|
||||
@JoinTable(name = "CODED_PAIR_HOLDER_PAIR_SET", joinColumns = @JoinColumn(name = "CODED_PAIR_HOLDER_ID"))
|
||||
@ForeignKey(name = "FK_PAIR_SET")
|
||||
private final Set<PersonPair> pairs = new HashSet<PersonPair>(0);
|
||||
|
||||
CodedPairHolder() {
|
||||
super();
|
||||
}
|
||||
|
||||
CodedPairHolder(final String pCode, final Set<PersonPair> pPersonPairs) {
|
||||
super();
|
||||
this.code = pCode;
|
||||
this.pairs.addAll(pPersonPairs);
|
||||
}
|
||||
|
||||
Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
String getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
Set<PersonPair> getPairs() {
|
||||
return Collections.unmodifiableSet(this.pairs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 101;
|
||||
int result = 1;
|
||||
result = prime * result + ((getCode() == null) ? 0 : getCode().hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object pObject) {
|
||||
if (this == pObject) {
|
||||
return true;
|
||||
}
|
||||
if (pObject == null) {
|
||||
return false;
|
||||
}
|
||||
if (!(pObject instanceof CodedPairHolder)) {
|
||||
return false;
|
||||
}
|
||||
final CodedPairHolder other = (CodedPairHolder) pObject;
|
||||
if (getCode() == null) {
|
||||
if (other.getCode() != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!getCode().equals(other.getCode())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,95 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, 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.test.annotations.cascade;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "PERSON")
|
||||
class Person implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 960914272100925312L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@Column(name = "ID")
|
||||
private Long id;
|
||||
|
||||
@Column(name = "NAME", nullable = false, unique = true, updatable = false, length = 256)
|
||||
private String name;
|
||||
|
||||
Person() {
|
||||
super();
|
||||
}
|
||||
|
||||
Person(final String pName) {
|
||||
super();
|
||||
this.name = pName;
|
||||
}
|
||||
|
||||
Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 103;
|
||||
int result = 1;
|
||||
result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object pObject) {
|
||||
if (this == pObject) {
|
||||
return true;
|
||||
}
|
||||
if (pObject == null) {
|
||||
return false;
|
||||
}
|
||||
if (!(pObject instanceof Person)) {
|
||||
return false;
|
||||
}
|
||||
final Person other = (Person) pObject;
|
||||
if (getName() == null) {
|
||||
if (other.getName() != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!getName().equals(other.getName())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2011, 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.test.annotations.cascade;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import org.hibernate.annotations.ForeignKey;
|
||||
|
||||
@Embeddable
|
||||
class PersonPair implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 4543565503074112720L;
|
||||
|
||||
@ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
|
||||
@JoinColumn(name = "LEFT_PERSON_ID", nullable = false, updatable = false)
|
||||
@ForeignKey(name = "FK_LEFT_PERSON")
|
||||
private Person left;
|
||||
|
||||
@ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH })
|
||||
@JoinColumn(name = "RIGHT_PERSON_ID", nullable = false, updatable = false)
|
||||
@ForeignKey(name = "FK_RIGHT_PERSON")
|
||||
private Person right;
|
||||
|
||||
PersonPair() {
|
||||
super();
|
||||
}
|
||||
|
||||
PersonPair(final Person pLeft, final Person pRight) {
|
||||
super();
|
||||
this.left = pLeft;
|
||||
this.right = pRight;
|
||||
}
|
||||
|
||||
Person getLeft() {
|
||||
return this.left;
|
||||
}
|
||||
|
||||
Person getRight() {
|
||||
return this.right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 107;
|
||||
int result = 1;
|
||||
result = prime * result + ((getLeft() == null) ? 0 : getLeft().hashCode());
|
||||
result = prime * result + ((getRight() == null) ? 0 : getRight().hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object pObject) {
|
||||
if (this == pObject) {
|
||||
return true;
|
||||
}
|
||||
if (pObject == null) {
|
||||
return false;
|
||||
}
|
||||
if (!(pObject instanceof PersonPair)) {
|
||||
return false;
|
||||
}
|
||||
final PersonPair other = (PersonPair) pObject;
|
||||
if (getRight() == null) {
|
||||
if (other.getRight() != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!getRight().equals(other.getRight())) {
|
||||
return false;
|
||||
}
|
||||
if (getLeft() == null) {
|
||||
if (other.getLeft() != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!getLeft().equals(other.getLeft())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue