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

Hibernate example source code file (ReadEntityAssociatedNotAuditedTest.java)

This example Hibernate source code file (ReadEntityAssociatedNotAuditedTest.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

abstractonesessiontest, car, car, hernan, io, mappingexception, net, network, person, person, personaje, personaje, readentityassociatednotauditedtest, string, test, test, urisyntaxexception

The Hibernate ReadEntityAssociatedNotAuditedTest.java source code

package org.hibernate.envers.test.integration.entityNames.singleAssociatedNotAudited;

import org.hibernate.MappingException;
import org.hibernate.envers.test.AbstractOneSessionTest;
import org.hibernate.envers.test.Priority;
import org.junit.Test;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * @author Hernán Chanfreau
 * 
 */
public class ReadEntityAssociatedNotAuditedTest extends AbstractOneSessionTest {

	private long id_car1;
	private long id_car2;
	
	private long id_pers1; 
	private long id_pers2; 
	
	private Car car1;
	private Car car2;
	private Person person1_1;
	private Person person2;
	private Person currentPerson1;
	private Car currentCar1;
	
	
	protected void initMappings() throws MappingException, URISyntaxException {
		URL url = Thread.currentThread().getContextClassLoader().getResource("mappings/entityNames/singleAssociatedNotAudited/mappings.hbm.xml");
        config.addFile(new File(url.toURI()));
	}

    @Test
    @Priority(10)
    public void initData() {
    	
    	initializeSession();

        Person pers1 = new Person("Hernan", 15);
        Person pers2 = new Person("Leandro", 19);
        
        Car car1 = new Car(1, pers1);
        Car car2 = new Car(2, pers2);
        
        //REV 1 
        getSession().getTransaction().begin();
        getSession().persist("Personaje",pers1);
        getSession().persist(car1);
        getSession().getTransaction().commit();
        id_car1 = car1.getId();
        id_pers1 = pers1.getId();

        //REV 2
        getSession().getTransaction().begin();
        pers1.setAge(50);
        getSession().persist("Personaje", pers1);
        getSession().persist("Personaje", pers2);
        getSession().persist(car2);
        getSession().getTransaction().commit();
        id_car2 = car2.getId();
        id_pers2 = pers2.getId();

    }
    
    private void loadDataOnSessionAndAuditReader() {
    	currentPerson1 = (Person)getSession().get("Personaje", id_pers1);
    	person2 = (Person)getSession().get("Personaje", id_pers2);
    	
    	currentCar1 = (Car)getSession().get(Car.class, id_car1);
    	
    	car1 = getAuditReader().find(Car.class, id_car1, 1);
    	car2 = getAuditReader().find(Car.class, id_car2, 2);
    	
    }
    
    private void checkEntityNames() {
    	
    	String currentCar1EN = getSession().getEntityName(currentCar1);
    	
    	String car1EN = getAuditReader().getEntityName(id_car1, 1, car1);
    	
    	assert (currentCar1EN.equals(car1EN));
    	
    }
    
    private void checkEntities() {

    	person1_1 = car1.getOwner();
    	Person person2_1 = car2.getOwner();
    	
    	assert(currentPerson1.getAge() == person1_1.getAge());
    	assert(person2.getAge() == person2_1.getAge());
    }
    
    @Test
    public void testObtainEntityNameAssociationWithEntityNameAndNotAuditedMode() {
    	loadDataOnSessionAndAuditReader();
    	
    	checkEntities();
    	
    	checkEntityNames();
    }
    

    @Test
    public void testObtainEntityNameAssociationWithEntityNameAndNotAuditedModeInNewSession() {
    	//force a new session and AR
    	forceNewSession();
    	
    	loadDataOnSessionAndAuditReader();
    	
    	checkEntities();
    	
    	checkEntityNames();

    }    

}

Other Hibernate examples (source code examples)

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