HHH-3218 : ManyToOne Persisting Cascade in Embeddable as part of ElementCollection

This commit is contained in:
Gail Badner 2012-01-18 00:39:35 -08:00
parent d1ac167ec6
commit 230cff7d00
4 changed files with 273 additions and 127 deletions

View File

@ -107,15 +107,41 @@ public class Property implements Serializable, MetaAttributable {
public CascadeStyle getCascadeStyle() throws MappingException {
Type type = value.getType();
if ( type.isComponentType() && !type.isAnyType() ) {
CompositeType actype = (CompositeType) type;
int length = actype.getSubtypes().length;
for ( int i=0; i<length; i++ ) {
if ( actype.getCascadeStyle(i)!=CascadeStyle.NONE ) return CascadeStyle.ALL;
}
return CascadeStyle.NONE;
if ( type.isComponentType() ) {
return getCompositeCascadeStyle( (CompositeType) type, cascade );
}
else if ( cascade==null || cascade.equals("none") ) {
else if ( type.isCollectionType() ) {
return getCollectionCascadeStyle( ( (Collection) value ).getElement().getType(), cascade );
}
else {
return getCascadeStyle( cascade );
}
}
private static CascadeStyle getCompositeCascadeStyle(CompositeType compositeType, String cascade) {
if ( compositeType.isAnyType() ) {
return getCascadeStyle( cascade );
}
int length = compositeType.getSubtypes().length;
for ( int i=0; i<length; i++ ) {
if ( compositeType.getCascadeStyle(i) != CascadeStyle.NONE ) {
return CascadeStyle.ALL;
}
}
return getCascadeStyle( cascade );
}
private static CascadeStyle getCollectionCascadeStyle(Type elementType, String cascade) {
if ( elementType.isComponentType() ) {
return getCompositeCascadeStyle( (CompositeType) elementType, cascade );
}
else {
return getCascadeStyle( cascade );
}
}
private static CascadeStyle getCascadeStyle(String cascade) {
if ( cascade==null || cascade.equals("none") ) {
return CascadeStyle.NONE;
}
else {
@ -126,9 +152,9 @@ public class Property implements Serializable, MetaAttributable {
styles[i++] = CascadeStyle.getCascadeStyle( tokens.nextToken() );
}
return new CascadeStyle.MultipleCascadeStyle(styles);
}
}
}
public String getCascade() {
return cascade;
}

View File

@ -30,22 +30,21 @@ import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
public class CascadeToElementCollectionEmbeddedManyToOneTest extends BaseCoreFunctionalTestCase {
public class CascadeToEmbeddedManyToOneTest extends BaseCoreFunctionalTestCase {
@Test
@FailureExpected( jiraKey = "HHH-3218" )
public void testPersistCascade() {
public void testPersistCascadeToSetOfEmbedded() {
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.persist( new CodedPairSetHolder( "CODE", setOfPairs ) );
sess.flush();
} finally {
trx.rollback();
@ -55,9 +54,27 @@ public class CascadeToElementCollectionEmbeddedManyToOneTest extends BaseCoreFun
}
}
@Test
public void testPersistCascadeToEmbedded() {
Session sess = openSession();
try {
final Transaction trx = sess.beginTransaction();
try {
PersonPair personPair = new PersonPair(new Person("PERSON NAME 1"), new Person("PERSON NAME 2"));
sess.persist( new CodedPairHolder( "CODE", personPair ) );
sess.flush();
} finally {
trx.rollback();
}
} finally {
sess.close();
}
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[]{
CodedPairSetHolder.class,
CodedPairHolder.class,
Person.class,
PersonPair.class

View File

@ -1,112 +1,103 @@
/*
* 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;
}
}
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import org.hibernate.annotations.ForeignKey;
@Entity
@Table(name = "CODED_PAIR_HOLDER")
class CodedPairHolder implements Serializable {
@Id
@GeneratedValue
@Column(name = "ID")
private Long id;
@Column(name = "CODE", nullable = false, unique = true, updatable = false, length = 256)
private String code;
private PersonPair pair;
CodedPairHolder() {
super();
}
CodedPairHolder(final String pCode, PersonPair pair) {
super();
this.code = pCode;
this.pair = pair;
}
Long getId() {
return this.id;
}
String getCode() {
return this.code;
}
PersonPair getPair() {
return this.pair;
}
@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;
}
}

View File

@ -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_SET_HOLDER")
class CodedPairSetHolder 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);
CodedPairSetHolder() {
super();
}
CodedPairSetHolder(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 CodedPairSetHolder )) {
return false;
}
final CodedPairSetHolder other = (CodedPairSetHolder) pObject;
if (getCode() == null) {
if (other.getCode() != null) {
return false;
}
} else if (!getCode().equals(other.getCode())) {
return false;
}
return true;
}
}