|
| 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.insertordering; |
| 8 | + |
| 9 | +import java.sql.PreparedStatement; |
| 10 | +import java.sql.SQLException; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import javax.persistence.CascadeType; |
| 15 | +import javax.persistence.Column; |
| 16 | +import javax.persistence.Entity; |
| 17 | +import javax.persistence.GeneratedValue; |
| 18 | +import javax.persistence.GenerationType; |
| 19 | +import javax.persistence.Id; |
| 20 | +import javax.persistence.ManyToMany; |
| 21 | +import javax.persistence.ManyToOne; |
| 22 | +import javax.persistence.SequenceGenerator; |
| 23 | + |
| 24 | +import org.hibernate.cfg.Environment; |
| 25 | + |
| 26 | +import org.hibernate.testing.TestForIssue; |
| 27 | +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
| 28 | +import org.hibernate.test.util.jdbc.PreparedStatementSpyConnectionProvider; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; |
| 32 | +import static org.junit.Assert.assertEquals; |
| 33 | +import static org.mockito.Mockito.times; |
| 34 | +import static org.mockito.Mockito.verify; |
| 35 | + |
| 36 | +/** |
| 37 | + * @author Vlad Mihalcea |
| 38 | + */ |
| 39 | +@TestForIssue(jiraKey = "HHH-11996") |
| 40 | +public class InsertOrderingWithMultipleManyToOne |
| 41 | +extends BaseNonConfigCoreFunctionalTestCase { |
| 42 | + |
| 43 | +private PreparedStatementSpyConnectionProvider connectionProvider = new PreparedStatementSpyConnectionProvider(); |
| 44 | + |
| 45 | +@Override |
| 46 | +protected Class[] getAnnotatedClasses() { |
| 47 | +return new Class[] { |
| 48 | +Parent.class, |
| 49 | +ChildA.class, |
| 50 | +ChildB.class, |
| 51 | +}; |
| 52 | +} |
| 53 | + |
| 54 | +@Override |
| 55 | +protected void addSettings(Map settings) { |
| 56 | +settings.put( Environment.ORDER_INSERTS, "true" ); |
| 57 | +settings.put( Environment.STATEMENT_BATCH_SIZE, "10" ); |
| 58 | +settings.put( |
| 59 | +org.hibernate.cfg.AvailableSettings.CONNECTION_PROVIDER, |
| 60 | +connectionProvider |
| 61 | +); |
| 62 | +} |
| 63 | + |
| 64 | +@Override |
| 65 | +public void releaseResources() { |
| 66 | +super.releaseResources(); |
| 67 | +connectionProvider.stop(); |
| 68 | +} |
| 69 | + |
| 70 | +@Test |
| 71 | +public void testBatching() throws SQLException { |
| 72 | +doInHibernate( this::sessionFactory, session -> { |
| 73 | +Parent parent = new Parent(); |
| 74 | +session.persist(parent); |
| 75 | + |
| 76 | +ChildA childA = new ChildA(); |
| 77 | +childA.setParent(parent); |
| 78 | +session.persist(childA); |
| 79 | + |
| 80 | +ChildB childB = new ChildB(); |
| 81 | +childB.setParent(parent); |
| 82 | +session.persist(childB); |
| 83 | + |
| 84 | +connectionProvider.clear(); |
| 85 | +} ); |
| 86 | + |
| 87 | +assertEquals( 3, connectionProvider.getPreparedStatements().size() ); |
| 88 | +/*PreparedStatement addressPreparedStatement = connectionProvider.getPreparedStatement( |
| 89 | +"insert into Address (ID) values (?)" ); |
| 90 | +verify( addressPreparedStatement, times( 2 ) ).addBatch(); |
| 91 | +verify( addressPreparedStatement, times( 1 ) ).executeBatch(); |
| 92 | +PreparedStatement personPreparedStatement = connectionProvider.getPreparedStatement( |
| 93 | +"insert into Person (ID) values (?)" ); |
| 94 | +verify( personPreparedStatement, times( 4 ) ).addBatch(); |
| 95 | +verify( personPreparedStatement, times( 1 ) ).executeBatch();*/ |
| 96 | +} |
| 97 | + |
| 98 | +@Entity(name = "Parent") |
| 99 | +public static class Parent { |
| 100 | +@Id |
| 101 | +@GeneratedValue |
| 102 | +private Integer id; |
| 103 | + |
| 104 | +public Integer getId() { |
| 105 | +return id; |
| 106 | +} |
| 107 | + |
| 108 | +public void setId(Integer id) { |
| 109 | +this.id = id; |
| 110 | +} |
| 111 | +} |
| 112 | + |
| 113 | +@Entity(name = "ChildA") |
| 114 | +public static class ChildA { |
| 115 | +@Id |
| 116 | +@GeneratedValue |
| 117 | +private Integer id; |
| 118 | + |
| 119 | +@ManyToOne |
| 120 | +private Parent parent; |
| 121 | + |
| 122 | +public Integer getId() { |
| 123 | +return id; |
| 124 | +} |
| 125 | + |
| 126 | +public void setId(Integer id) { |
| 127 | +this.id = id; |
| 128 | +} |
| 129 | + |
| 130 | +public Parent getParent() { |
| 131 | +return parent; |
| 132 | +} |
| 133 | + |
| 134 | +public void setParent(Parent parent) { |
| 135 | +this.parent = parent; |
| 136 | +} |
| 137 | +} |
| 138 | + |
| 139 | +@Entity(name = "ChildB") |
| 140 | +public static class ChildB { |
| 141 | +@Id |
| 142 | +@GeneratedValue |
| 143 | +private Integer id; |
| 144 | + |
| 145 | +@ManyToOne |
| 146 | +private Parent parent; |
| 147 | + |
| 148 | +public Integer getId() { |
| 149 | +return id; |
| 150 | +} |
| 151 | + |
| 152 | +public void setId(Integer id) { |
| 153 | +this.id = id; |
| 154 | +} |
| 155 | + |
| 156 | +public Parent getParent() { |
| 157 | +return parent; |
| 158 | +} |
| 159 | + |
| 160 | +public void setParent(Parent parent) { |
| 161 | +this.parent = parent; |
| 162 | +} |
| 163 | +} |
| 164 | +} |
0 commit comments