From 600369413da207e9460a04e0cdc18c94c6514618 Mon Sep 17 00:00:00 2001 From: mukhanov Date: Sun, 12 Jan 2014 15:06:08 +0400 Subject: [PATCH] HHH-3482 Fixed StatelessSession inserting bug + test --- .../internal/SessionFactoryImpl.java | 2 +- .../internal/StatelessSessionImpl.java | 6 +- .../test/stateless/insert/Mappings.hbm.xml | 47 +++++++++++ .../test/stateless/insert/Message.java | 35 ++++++++ .../stateless/insert/MessageRecipient.java | 35 ++++++++ .../insert/StatelessSessionInsertTest.java | 81 +++++++++++++++++++ 6 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/stateless/insert/Mappings.hbm.xml create mode 100644 hibernate-core/src/test/java/org/hibernate/test/stateless/insert/Message.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/stateless/insert/MessageRecipient.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/stateless/insert/StatelessSessionInsertTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java index 05c08f5f79..939b223fef 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionFactoryImpl.java @@ -1718,7 +1718,7 @@ public final class SessionFactoryImpl @Override public StatelessSession openStatelessSession() { - return new StatelessSessionImpl( connection, tenantIdentifier, sessionFactory ); + return new StatelessSessionImpl( connection, tenantIdentifier, sessionFactory, sessionFactory.settings.getRegionFactory().nextTimestamp() ); } @Override diff --git a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java index dfb1d62fef..1ac632d00f 100755 --- a/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/StatelessSessionImpl.java @@ -83,10 +83,12 @@ public class StatelessSessionImpl extends AbstractSessionImpl implements Statele private TransactionCoordinator transactionCoordinator; private PersistenceContext temporaryPersistenceContext = new StatefulPersistenceContext( this ); + private long timestamp; - StatelessSessionImpl(Connection connection, String tenantIdentifier, SessionFactoryImpl factory) { + StatelessSessionImpl(Connection connection, String tenantIdentifier, SessionFactoryImpl factory, long timestamp) { super( factory, tenantIdentifier ); this.transactionCoordinator = new TransactionCoordinatorImpl( connection, this ); + this.timestamp = timestamp; } // TransactionContext ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -531,7 +533,7 @@ public class StatelessSessionImpl extends AbstractSessionImpl implements Statele @Override public long getTimestamp() { - throw new UnsupportedOperationException(); + return timestamp; } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/test/stateless/insert/Mappings.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/stateless/insert/Mappings.hbm.xml new file mode 100644 index 0000000000..8a2534b4ae --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/stateless/insert/Mappings.hbm.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/hibernate-core/src/test/java/org/hibernate/test/stateless/insert/Message.java b/hibernate-core/src/test/java/org/hibernate/test/stateless/insert/Message.java new file mode 100644 index 0000000000..3cb0bf9b79 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/stateless/insert/Message.java @@ -0,0 +1,35 @@ +package org.hibernate.test.stateless.insert; + +/** + * @author mukhanov@gmail.com + */ +public class Message { + + private String id; + private String subject; + private String content; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/stateless/insert/MessageRecipient.java b/hibernate-core/src/test/java/org/hibernate/test/stateless/insert/MessageRecipient.java new file mode 100644 index 0000000000..a7b8d66a5e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/stateless/insert/MessageRecipient.java @@ -0,0 +1,35 @@ +package org.hibernate.test.stateless.insert; + +/** + * @author mukhanov@gmail.com + */ +public class MessageRecipient { + + private String id; + private String email; + private Message message; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public Message getMessage() { + return message; + } + + public void setMessage(Message message) { + this.message = message; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/stateless/insert/StatelessSessionInsertTest.java b/hibernate-core/src/test/java/org/hibernate/test/stateless/insert/StatelessSessionInsertTest.java new file mode 100644 index 0000000000..c1b9e62333 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/stateless/insert/StatelessSessionInsertTest.java @@ -0,0 +1,81 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2009-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.stateless.insert; + +import org.hibernate.Session; +import org.hibernate.StatelessSession; +import org.hibernate.Transaction; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.jboss.logging.Logger; +import org.junit.Test; + +/** + * @author mukhanov@gmail.com + */ +public class StatelessSessionInsertTest extends BaseCoreFunctionalTestCase { + private static final Logger log = Logger.getLogger(StatelessSessionInsertTest.class); + + @Override + public String[] getMappings() { + return new String[]{"stateless/insert/Mappings.hbm.xml"}; + } + + @Test + public void testInsertWithForeignKey() { + Session session = sessionFactory().openSession(); + Transaction tx = session.beginTransaction(); + + Message msg = new Message(); + final String messageId = "message_id"; + msg.setId(messageId); + msg.setContent("message_content"); + msg.setSubject("message_subject"); + session.save(msg); + + tx.commit(); + session.close(); + + StatelessSession statelessSession = sessionFactory().openStatelessSession(); + tx = statelessSession.beginTransaction(); + + MessageRecipient signature = new MessageRecipient(); + signature.setId("recipient"); + signature.setEmail("recipient@hibernate.org"); + signature.setMessage(msg); + statelessSession.insert(signature); + + tx.commit(); + + cleanup(); + } + + private void cleanup() { + Session s = openSession(); + s.beginTransaction(); + s.createQuery("delete MessageRecipient").executeUpdate(); + s.createQuery("delete Message").executeUpdate(); + s.getTransaction().commit(); + s.close(); + } +}