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

Hibernate example source code file (DifferentDBSchemaTest.java)

This example Hibernate source code file (DifferentDBSchemaTest.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, differentdbschematest, envers_audit, if, interruptedexception, override, override, schema_name, schema_name, strtestentity, strtestentity, table, test, test, util

The Hibernate DifferentDBSchemaTest.java source code

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

import org.hibernate.cfg.Environment;
import org.hibernate.ejb.Ejb3Configuration;
import org.hibernate.envers.test.AbstractEntityTest;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.entities.StrTestEntity;
import org.hibernate.mapping.Table;
import org.junit.Test;

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

/**
 * Tests simple auditing process (read and write operations) when <i>REVINFO and audit tables
 * exist in a different database schema.
 * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
 */
public class DifferentDBSchemaTest extends AbstractEntityTest {
    private static final String SCHEMA_NAME = "ENVERS_AUDIT";
    private Integer steId = null;

    @Override
    public void addConfigurationProperties(Properties configuration) {
        super.addConfigurationProperties(configuration);

        // Creates new schema after establishing connection
        configuration.setProperty(Environment.URL, configuration.getProperty(Environment.URL) + ";INIT=CREATE SCHEMA IF NOT EXISTS " + SCHEMA_NAME);
        configuration.setProperty("org.hibernate.envers.default_schema", SCHEMA_NAME);
    }

    @Override
    public void configure(Ejb3Configuration cfg) {
        cfg.addAnnotatedClass(StrTestEntity.class);
    }

    @Test
    @Priority(10)
    public void initData() throws InterruptedException {
        // Revision 1
        EntityManager em = getEntityManager();
        em.getTransaction().begin();
        StrTestEntity ste = new StrTestEntity("x");
        em.persist(ste);
        steId = ste.getId();
        em.getTransaction().commit();

        // Revision 2
        em.getTransaction().begin();
        ste = em.find(StrTestEntity.class, steId);
        ste.setStr("y");
        em.getTransaction().commit();
    }

    @Test
    public void testRevinfoSchemaName() {
        Table revisionTable = getCfg().getClassMapping("org.hibernate.envers.DefaultRevisionEntity").getTable();
        assert SCHEMA_NAME.equals(revisionTable.getSchema());
    }

    @Test
    public void testRevisionsCounts() {
        assert Arrays.asList(1, 2).equals(getAuditReader().getRevisions(StrTestEntity.class, steId));
    }

    @Test
    public void testHistoryOfId1() {
        StrTestEntity ver1 = new StrTestEntity("x", steId);
        StrTestEntity ver2 = new StrTestEntity("y", steId);

        assert getAuditReader().find(StrTestEntity.class, steId, 1).equals(ver1);
        assert getAuditReader().find(StrTestEntity.class, steId, 2).equals(ver2);
    }
}

Other Hibernate examples (source code examples)

Here is a short list of links related to this Hibernate DifferentDBSchemaTest.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.