alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Hibernate example source code file (JtaTransaction.java)

This example Hibernate source code file (JtaTransaction.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Hibernate tags/keywords

abstractentitytest, exception, failureexpected, failureexpected, hhh-6624, hhh-6624, integer, inttestentity, inttestentity, jtatransaction, override, test, test, transaction, transactionmanager, util

The Hibernate JtaTransaction.java source code

package org.hibernate.envers.test.integration.jta;

import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.EnversTestingJtaBootstrap;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.IntTestEntity;
import org.hibernate.testing.FailureExpected;

import org.junit.Test;

import javax.persistence.EntityManager;
import javax.transaction.TransactionManager;
import java.util.Arrays;
import java.util.Properties;

import static org.hibernate.envers.test.EnversTestingJtaBootstrap.*;

/**
 * Same as {@link org.hibernate.envers.test.integration.basic.Simple}, but in a JTA environment.
 * @author Adam Warski (adam at warski dot org)
 */
public class JtaTransaction extends AbstractEntityTest {
    private TransactionManager tm;
    private Integer id1;

    public void configure(Ejb3Configuration cfg) {
        cfg.addAnnotatedClass(IntTestEntity.class);
    }

    @Override
    public void addConfigurationProperties(Properties configuration) {
        super.addConfigurationProperties(configuration);
        tm = EnversTestingJtaBootstrap.updateConfigAndCreateTM(configuration);
    }

    @Test
    @Priority(10)
    @FailureExpected(jiraKey = "HHH-6624")
    public void initData() throws Exception {
        tm.begin();

        EntityManager em;
        IntTestEntity ite;
        try {
            newEntityManager();
            em = getEntityManager();
            ite = new IntTestEntity(10);
            em.persist(ite);
            id1 = ite.getId();
        } finally {
            tryCommit(tm);
        }

        //

        tm.begin();

        try {
            newEntityManager();
            em = getEntityManager();
            ite = em.find(IntTestEntity.class, id1);
            ite.setNumber(20);
        } finally {
            tryCommit(tm);
        }
    }

    @Test
	@FailureExpected(jiraKey = "HHH-6624")
    public void testRevisionsCounts() throws Exception {
        assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(IntTestEntity.class, id1));
    }

    @Test
	@FailureExpected(jiraKey = "HHH-6624")
    public void testHistoryOfId1() {
        IntTestEntity ver1 = new IntTestEntity(10, id1);
        IntTestEntity ver2 = new IntTestEntity(20, id1);

        assert getAuditReader().find(IntTestEntity.class, id1, 1).equals(ver1);
        assert getAuditReader().find(IntTestEntity.class, id1, 2).equals(ver2);
    }
}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate JtaTransaction.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.