HHH-3482 Fixed StatelessSession inserting bug + test

This commit is contained in:
mukhanov 2014-01-12 15:06:08 +04:00 committed by Brett Meyer
parent 30cfb41174
commit 5b61a3b46e
6 changed files with 203 additions and 3 deletions

View File

@ -1763,7 +1763,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

View File

@ -84,10 +84,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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -532,7 +534,7 @@ public class StatelessSessionImpl extends AbstractSessionImpl implements Statele
@Override
public long getTimestamp() {
throw new UnsupportedOperationException();
return timestamp;
}
@Override

View File

@ -0,0 +1,47 @@
<?xml version="1.0"?>
<!--
~ Copyright (c) 2009, Red Hat Middleware LLC 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 Middleware LLC.
~
~ 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
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.stateless.insert">
<class name="Message">
<cache usage="read-write"/>
<id name="id">
<generator class="assigned"/>
</id>
<property name="subject"/>
<property name="content"/>
</class>
<class name="MessageRecipient">
<cache usage="read-write"/>
<id name="id">
<generator class="assigned"/>
</id>
<property name="email"/>
<many-to-one name="message" foreign-key="fk_message_recipient"/>
</class>
</hibernate-mapping>

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}