HHH-9979 : Add tests using bean validation, and PRE_INSERT and POST_INSERT event listeners
This commit is contained in:
parent
287a516760
commit
71f4f3272a
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* 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.annotations.beanvalidation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Ryan Emerson
|
||||
*/
|
||||
@TestForIssue( jiraKey = "HHH-9979")
|
||||
public class MergeNotNullCollectionTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {Parent.class, Child.class};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(Configuration cfg) {
|
||||
super.configure( cfg );
|
||||
cfg.setProperty( "javax.persistence.validation.mode", "AUTO" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneToManyNotNullCollection() {
|
||||
Parent parent = new Parent();
|
||||
parent.id = 1L;
|
||||
Child child = new Child();
|
||||
child.id = 1L;
|
||||
|
||||
List<Child> children = new ArrayList<Child>();
|
||||
children.add( child );
|
||||
|
||||
child.setParent( parent );
|
||||
parent.setChildren( children );
|
||||
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
parent = (Parent) s.merge( parent );
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
s.delete( parent );
|
||||
t.commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test(expected = ConstraintViolationException.class)
|
||||
public void testOneToManyNullCollection() {
|
||||
Parent parent = new Parent();
|
||||
parent.id = 1L;
|
||||
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
parent = (Parent) s.merge( parent );
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
s.delete( parent );
|
||||
t.commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "PARENT")
|
||||
static class Parent {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@NotNull
|
||||
private List<Child> children;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<Child> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(List<Child> children) {
|
||||
this.children = children;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "CHILD")
|
||||
static class Child {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
private Parent parent;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Parent getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(Parent parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
* 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.annotations.beanvalidation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
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.Table;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.testing.DialectChecks;
|
||||
import org.hibernate.testing.FailureExpected;
|
||||
import org.hibernate.testing.RequiresDialectFeature;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
/**
|
||||
* @author Ryan Emerson
|
||||
*/
|
||||
@RequiresDialectFeature( value = DialectChecks.SupportsIdentityColumns.class, jiraKey = "HHH-9979")
|
||||
@TestForIssue( jiraKey = "HHH-9979")
|
||||
public class MergeNotNullCollectionUsingIdentityTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {Parent.class, Child.class};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(Configuration cfg) {
|
||||
super.configure( cfg );
|
||||
cfg.setProperty( "javax.persistence.validation.mode", "AUTO" );
|
||||
}
|
||||
|
||||
@Test
|
||||
@FailureExpected(jiraKey = "HHH-9979")
|
||||
public void testOneToManyNotNullCollection() {
|
||||
Parent parent = new Parent();
|
||||
Child child = new Child();
|
||||
|
||||
List<Child> children = new ArrayList<Child>();
|
||||
children.add( child );
|
||||
|
||||
child.setParent( parent );
|
||||
parent.setChildren( children );
|
||||
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
parent = (Parent) s.merge( parent );
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
s.delete( parent );
|
||||
t.commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Test(expected = ConstraintViolationException.class)
|
||||
public void testOneToManyNullCollection() {
|
||||
Parent parent = new Parent();
|
||||
Child child = new Child();
|
||||
child.setParent( parent );
|
||||
|
||||
Session s = openSession();
|
||||
Transaction t = s.beginTransaction();
|
||||
parent = (Parent) s.merge( parent );
|
||||
t.commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
t = s.beginTransaction();
|
||||
s.delete( parent );
|
||||
t.commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "PARENT")
|
||||
static class Parent {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@NotNull
|
||||
private List<Child> children;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<Child> getChildren() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public void setChildren(List<Child> children) {
|
||||
this.children = children;
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
@Table(name = "CHILD")
|
||||
static class Child {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
private Parent parent;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Parent getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(Parent parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
* 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.event.entity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.event.service.spi.EventListenerRegistry;
|
||||
import org.hibernate.event.spi.EventType;
|
||||
import org.hibernate.event.spi.PostInsertEvent;
|
||||
import org.hibernate.event.spi.PostInsertEventListener;
|
||||
import org.hibernate.event.spi.PreInsertEvent;
|
||||
import org.hibernate.event.spi.PreInsertEventListener;
|
||||
import org.hibernate.persister.entity.EntityPersister;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Gail Badner
|
||||
*/
|
||||
@TestForIssue( jiraKey = "HHH-9979")
|
||||
public class MergeListPreAndPostPersistTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] {
|
||||
Order.class,
|
||||
Item.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-9979")
|
||||
public void testAllPropertiesCopied() {
|
||||
final Order order = new Order();
|
||||
order.id = 1L;
|
||||
order.name = "order";
|
||||
Item item = new Item();
|
||||
item.id = 1L;
|
||||
item.name = "item";
|
||||
order.items.add( item );
|
||||
|
||||
addEntityListeners( order );
|
||||
|
||||
Session s = openSession();
|
||||
s.getTransaction().begin();
|
||||
s.merge( order );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.getTransaction().begin();
|
||||
s.delete( order );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Entity
|
||||
private static class Order {
|
||||
@Id
|
||||
public Long id;
|
||||
|
||||
@Basic(optional = false)
|
||||
public String name;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
public List<Item> items = new ArrayList<Item>();
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Order order = (Order) o;
|
||||
|
||||
return name.equals( order.name );
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
private static class Item {
|
||||
@Id
|
||||
public Long id;
|
||||
|
||||
@Basic(optional = false)
|
||||
public String name;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Item item = (Item) o;
|
||||
|
||||
return name.equals( item.name );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
private void addEntityListeners(final Order order) {
|
||||
|
||||
EventListenerRegistry registry = sessionFactory().getServiceRegistry()
|
||||
.getService( EventListenerRegistry.class );
|
||||
registry.setListeners(
|
||||
EventType.PRE_INSERT,
|
||||
new PreInsertEventListener() {
|
||||
@Override
|
||||
public boolean onPreInsert(PreInsertEvent event) {
|
||||
if ( Order.class.isInstance( event.getEntity() ) ) {
|
||||
assertEquals( order, event.getEntity());
|
||||
assertEquals( order.items, ( (Order) event.getEntity() ).items );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
registry.setListeners(
|
||||
EventType.POST_INSERT,
|
||||
new PostInsertEventListener() {
|
||||
public void onPostInsert(PostInsertEvent event) {
|
||||
if ( Order.class.isInstance( event.getEntity() ) ) {
|
||||
assertEquals( order, event.getEntity());
|
||||
assertEquals( order.items, ( (Order) event.getEntity() ).items );
|
||||
}
|
||||
}
|
||||
|
||||
public boolean requiresPostCommitHanding(EntityPersister persister) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,174 @@
|
|||
/*
|
||||
* 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.event.entity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.event.service.spi.EventListenerRegistry;
|
||||
import org.hibernate.event.spi.EventType;
|
||||
import org.hibernate.event.spi.PostInsertEvent;
|
||||
import org.hibernate.event.spi.PostInsertEventListener;
|
||||
import org.hibernate.event.spi.PreInsertEvent;
|
||||
import org.hibernate.event.spi.PreInsertEventListener;
|
||||
import org.hibernate.persister.entity.EntityPersister;
|
||||
import org.hibernate.testing.DialectCheck;
|
||||
import org.hibernate.testing.DialectChecks;
|
||||
import org.hibernate.testing.FailureExpected;
|
||||
import org.hibernate.testing.RequiresDialectFeature;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Gail Badner
|
||||
*/
|
||||
@TestForIssue( jiraKey = "HHH-9979")
|
||||
@RequiresDialectFeature( value = DialectChecks.SupportsIdentityColumns.class, jiraKey = "HHH-9918")
|
||||
public class MergeListPreAndPostPersistWithIdentityTest extends BaseCoreFunctionalTestCase {
|
||||
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] {
|
||||
Order.class,
|
||||
Item.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-9979")
|
||||
@FailureExpected( jiraKey = "HHH-9979")
|
||||
public void testAllPropertiesCopied() {
|
||||
final Order order = new Order();
|
||||
order.id = 1L;
|
||||
order.name = "order";
|
||||
// Item.id is an identity so don't initialize it.
|
||||
Item item = new Item();
|
||||
item.name = "item";
|
||||
order.items.add( item );
|
||||
|
||||
addEntityListeners( order );
|
||||
|
||||
Session s = openSession();
|
||||
s.getTransaction().begin();
|
||||
s.merge( order );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
s = openSession();
|
||||
s.getTransaction().begin();
|
||||
s.delete( order );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
@Entity
|
||||
private static class Order {
|
||||
@Id
|
||||
public Long id;
|
||||
|
||||
@Basic(optional = false)
|
||||
public String name;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
public List<Item> items = new ArrayList<Item>();
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Order order = (Order) o;
|
||||
|
||||
return name.equals( order.name );
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
@Entity
|
||||
private static class Item {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
public Long id;
|
||||
|
||||
@Basic(optional = false)
|
||||
public String name;
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if ( this == o ) {
|
||||
return true;
|
||||
}
|
||||
if ( o == null || getClass() != o.getClass() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Item item = (Item) o;
|
||||
|
||||
return name.equals( item.name );
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
}
|
||||
}
|
||||
|
||||
private void addEntityListeners(final Order order) {
|
||||
|
||||
EventListenerRegistry registry = sessionFactory().getServiceRegistry()
|
||||
.getService( EventListenerRegistry.class );
|
||||
registry.setListeners(
|
||||
EventType.PRE_INSERT,
|
||||
new PreInsertEventListener() {
|
||||
@Override
|
||||
public boolean onPreInsert(PreInsertEvent event) {
|
||||
if ( Order.class.isInstance( event.getEntity() ) ) {
|
||||
assertEquals( order, event.getEntity());
|
||||
assertEquals( order.items, ( (Order) event.getEntity() ).items );
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
registry.setListeners(
|
||||
EventType.POST_INSERT,
|
||||
new PostInsertEventListener() {
|
||||
public void onPostInsert(PostInsertEvent event) {
|
||||
if ( Order.class.isInstance( event.getEntity() ) ) {
|
||||
assertEquals( order, event.getEntity());
|
||||
assertEquals( order.items, ( (Order) event.getEntity() ).items );
|
||||
}
|
||||
}
|
||||
|
||||
public boolean requiresPostCommitHanding(EntityPersister persister) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue