|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.test.collection.list; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import javax.persistence.CascadeType; |
| 12 | +import javax.persistence.Column; |
| 13 | +import javax.persistence.Entity; |
| 14 | +import javax.persistence.FetchType; |
| 15 | +import javax.persistence.GeneratedValue; |
| 16 | +import javax.persistence.Id; |
| 17 | +import javax.persistence.JoinColumn; |
| 18 | +import javax.persistence.ManyToOne; |
| 19 | +import javax.persistence.OneToMany; |
| 20 | +import javax.persistence.OrderColumn; |
| 21 | +import javax.persistence.Table; |
| 22 | + |
| 23 | +import org.hibernate.Session; |
| 24 | + |
| 25 | +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
| 26 | +import org.junit.After; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import static junit.framework.Assert.assertEquals; |
| 31 | + |
| 32 | +/** |
| 33 | + * Test initially developed for HHH-9195 |
| 34 | + * @author Steve Ebersole |
| 35 | + */ |
| 36 | +public class ListIndexReferenceFromListElementTest extends BaseNonConfigCoreFunctionalTestCase { |
| 37 | + @Entity( name = "LocalOrder" ) |
| 38 | + @Table( name = "LocalOrder" ) |
| 39 | + public static class LocalOrder { |
| 40 | + @Id |
| 41 | + @GeneratedValue |
| 42 | + public Integer id; |
| 43 | + @OneToMany( mappedBy = "order", cascade = CascadeType.ALL, fetch = FetchType.LAZY ) |
| 44 | + @OrderColumn( name = "position" ) |
| 45 | + public List<LocalLineItem> lineItems = new ArrayList<LocalLineItem>(); |
| 46 | + |
| 47 | + public LocalOrder() { |
| 48 | + } |
| 49 | + |
| 50 | + public LocalLineItem makeLineItem(String name) { |
| 51 | + LocalLineItem lineItem = new LocalLineItem( name, this ); |
| 52 | + lineItems.add( lineItem ); |
| 53 | + return lineItem; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Entity( name = "LocalLineItem" ) |
| 58 | + @Table( name = "LocalLineItem" ) |
| 59 | + public static class LocalLineItem { |
| 60 | + @Id |
| 61 | + @GeneratedValue |
| 62 | + public Integer id; |
| 63 | + public String name; |
| 64 | + @ManyToOne |
| 65 | + @JoinColumn |
| 66 | + public LocalOrder order; |
| 67 | + @Column( insertable = false, updatable = false ) |
| 68 | + public int position; |
| 69 | + |
| 70 | + public LocalLineItem() { |
| 71 | + } |
| 72 | + |
| 73 | + public LocalLineItem(String name, LocalOrder order) { |
| 74 | + this.name = name; |
| 75 | + this.order = order; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + protected Class[] getAnnotatedClasses() { |
| 81 | + return new Class[] { LocalOrder.class, LocalLineItem.class }; |
| 82 | + } |
| 83 | + |
| 84 | + @Before |
| 85 | + public void before() { |
| 86 | + Session s = sessionFactory().openSession(); |
| 87 | + s.beginTransaction(); |
| 88 | + LocalOrder localOrder = new LocalOrder(); |
| 89 | + localOrder.makeLineItem( "Shoes" ); |
| 90 | + localOrder.makeLineItem( "Socks" ); |
| 91 | + s.save( localOrder ); |
| 92 | + s.getTransaction().commit(); |
| 93 | + s.close(); |
| 94 | + } |
| 95 | + |
| 96 | + @After |
| 97 | + public void after() { |
| 98 | + Session s = sessionFactory().openSession(); |
| 99 | + s.beginTransaction(); |
| 100 | + s.createQuery( "delete LocalLineItem" ).executeUpdate(); |
| 101 | + s.createQuery( "delete LocalOrder" ).executeUpdate(); |
| 102 | + s.getTransaction().commit(); |
| 103 | + s.close(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testIt() { |
| 108 | + { |
| 109 | + Session s = openSession(); |
| 110 | + s.beginTransaction(); |
| 111 | + LocalOrder order = s.byId( LocalOrder.class ).load( 1 ); |
| 112 | + assertEquals( 2, order.lineItems.size() ); |
| 113 | + LocalLineItem shoes = order.lineItems.get( 0 ); |
| 114 | + LocalLineItem socks = order.lineItems.get( 1 ); |
| 115 | + assertEquals( "Shoes", shoes.name ); |
| 116 | + assertEquals( 0, shoes.position ); |
| 117 | + assertEquals( 1, socks.position ); |
| 118 | + order.lineItems.remove( socks ); |
| 119 | + order.lineItems.add( 0, socks ); |
| 120 | + s.getTransaction().commit(); |
| 121 | + s.close(); |
| 122 | + } |
| 123 | + |
| 124 | + { |
| 125 | + Session s = openSession(); |
| 126 | + s.beginTransaction(); |
| 127 | + LocalOrder order = s.byId( LocalOrder.class ).load( 1 ); |
| 128 | + assertEquals( 2, order.lineItems.size() ); |
| 129 | + LocalLineItem socks = order.lineItems.get( 0 ); |
| 130 | + LocalLineItem shoes = order.lineItems.get( 1 ); |
| 131 | + assertEquals( "Shoes", shoes.name ); |
| 132 | + assertEquals( 0, socks.position ); |
| 133 | + assertEquals( 1, shoes.position ); |
| 134 | + |
| 135 | + s.getTransaction().commit(); |
| 136 | + s.close(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments