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

Hibernate example source code file (EntityNamesTest.java)

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

abstractsessiontest, arraylist, camomo, car, car, david, hernan, io, mappingexception, net, network, person, person, personaje, personaje, test, test, util

The Hibernate EntityNamesTest.java source code

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

import org.hibernate.MappingException;
import org.hibernate.envers.test.AbstractSessionTest;
import org.hibernate.envers.test.Priority;
import org.hibernate.envers.test.integration.entityNames.manyToManyAudited.Car;
import org.hibernate.envers.test.integration.entityNames.manyToManyAudited.Person;
import org.hibernate.envers.test.tools.TestTools;
import org.hibernate.envers.tools.Pair;
import org.junit.Test;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
 */
public class EntityNamesTest extends AbstractSessionTest {
	protected void initMappings() throws MappingException, URISyntaxException {
		URL url = Thread.currentThread().getContextClassLoader().getResource("mappings/entityNames/manyToManyAudited/mappings.hbm.xml");
        config.addFile(new File(url.toURI()));
        config.setProperty("org.hibernate.envers.track_entities_changed_in_revision", "true");
	}

    @Test
    @Priority(10)
    public void initData() {
        Person pers1 = new Person("Hernan", 28);
        Person pers2 = new Person("Leandro", 29);
        Person pers3 = new Person("Barba", 32);
        Person pers4 = new Person("Camomo", 15);

        // Revision 1
        getSession().getTransaction().begin();
        List<Person > owners = new ArrayList();
        owners.add(pers1);
        owners.add(pers2);
        owners.add(pers3);
        Car car1 = new Car(5, owners);
        getSession().persist(car1);
        getSession().getTransaction().commit();
        long person1Id = pers1.getId();

        // Revision 2
        owners = new ArrayList<Person>();
        owners.add(pers2);
        owners.add(pers3);
        owners.add(pers4);
        Car car2 = new Car(27, owners);
        getSession().getTransaction().begin();
        Person person1 = (Person)getSession().get("Personaje", person1Id);
        person1.setName("Hernan David");
        person1.setAge(40);
        getSession().persist(car1);
        getSession().persist(car2);
        getSession().getTransaction().commit();
    }

    @Test
    @SuppressWarnings("unchecked")
    public void testModifiedEntityTypes() {
        assert TestTools.makeSet(Pair.make(Car.class.getName(), Car.class),
                                 Pair.make("Personaje", Person.class))
                        .equals(getAuditReader().getCrossTypeRevisionChangesReader().findEntityTypes(1));
        assert TestTools.makeSet(Pair.make(Car.class.getName(), Car.class),
                                 Pair.make("Personaje", Person.class))
                        .equals(getAuditReader().getCrossTypeRevisionChangesReader().findEntityTypes(2));
    }
}

Other Hibernate examples (source code examples)

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