Created
February 2, 2015 21:55
-
-
Save jeremija/0e90c1a1d4f23a0c8e29 to your computer and use it in GitHub Desktop.
Hibernate Test case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package test.hibernate; | |
| import org.hibernate.Session; | |
| import org.hibernate.SessionFactory; | |
| import org.hibernate.boot.registry.StandardServiceRegistryBuilder; | |
| import org.hibernate.cfg.Configuration; | |
| import org.hibernate.criterion.CriteriaSpecification; | |
| import org.hibernate.criterion.Restrictions; | |
| import org.hibernate.service.ServiceRegistry; | |
| import org.junit.Before; | |
| import org.junit.Test; | |
| import javax.persistence.*; | |
| import java.util.Arrays; | |
| import java.util.HashSet; | |
| import java.util.List; | |
| import java.util.Set; | |
| import static junit.framework.TestCase.assertEquals; | |
| import static junit.framework.TestCase.assertNotNull; | |
| public class HibernateTest { | |
| public class HibernateConfig { | |
| public SessionFactory buildSessionFactory() { | |
| Configuration configuration = new Configuration() | |
| .addAnnotatedClass(Owner.class) | |
| .addAnnotatedClass(Car.class) | |
| .setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect") | |
| .setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1") | |
| .setProperty("hibernate.current_session_context_class", "thread") | |
| .setProperty("hibernate.show_sql", "true") | |
| .setProperty("hibernate.hbm2ddl.auto", "create"); | |
| ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() | |
| .applySettings(configuration.getProperties()).build(); | |
| return configuration.buildSessionFactory(serviceRegistry); | |
| } | |
| } | |
| @Entity | |
| @Table(name = "owner") | |
| static class Owner { | |
| @Id @GeneratedValue | |
| Integer id; | |
| String name; | |
| @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) | |
| @JoinColumn(name = "ownerId") | |
| Set<Car> cars; | |
| protected Owner() {} | |
| public Owner(String name, Set<Car> cars) { | |
| this.name = name; | |
| this.cars = cars; | |
| } | |
| @Override public String toString() { | |
| return "Owner{name=" + name + ", cars=" + cars + "}"; | |
| } | |
| } | |
| @Entity | |
| @Table(name = "car") | |
| static class Car { | |
| @Id @GeneratedValue | |
| Integer id; | |
| String brand; | |
| Integer ownerId; | |
| protected Car() {} | |
| public Car(String brand) { | |
| this.brand = brand; | |
| } | |
| @Override public String toString() { | |
| return "Car{brand=" + brand + "}"; | |
| } | |
| } | |
| SessionFactory sessionFactory; | |
| @Before | |
| public void setup() { | |
| sessionFactory = new HibernateConfig().buildSessionFactory(); | |
| addTestData(); | |
| } | |
| private void addTestData() { | |
| Session session = sessionFactory.getCurrentSession(); | |
| session.beginTransaction(); | |
| Owner owner1 = new Owner("john", new HashSet<Car>(Arrays.asList( | |
| new Car("audi"), new Car("opel"), new Car("porsche")))); | |
| Owner owner2 = new Owner("jack", new HashSet<Car>(Arrays.asList( | |
| new Car("audi"), new Car("audi"), new Car("mercedes")))); | |
| Owner owner3 = new Owner("jeff", new HashSet<Car>(Arrays.asList( | |
| new Car("ferrari")))); | |
| session.save(owner1); | |
| session.save(owner2); | |
| session.save(owner3); | |
| session.getTransaction().commit(); | |
| } | |
| @Test | |
| @SuppressWarnings("unchecked") | |
| public void test() { | |
| System.out.println("--- test begin ---"); | |
| Session session = sessionFactory.getCurrentSession(); | |
| session.beginTransaction(); | |
| List<Owner> owners = (List<Owner>) session.createCriteria(Owner.class) | |
| .createCriteria("cars") | |
| .add(Restrictions.eq("brand", "audi")) | |
| .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY) | |
| .list(); | |
| System.out.println(owners); | |
| assertNotNull("owners should be defined", owners); | |
| assertEquals("should return owners who own an audi", 2, owners.size()); | |
| for(Owner owner : owners) | |
| for(Car car : owner.cars) | |
| assertEquals("all cars should be audi", "audi", car.brand); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>test.hibernate</groupId> | |
| <artifactId>hibernate-test</artifactId> | |
| <name>hibernate-test</name> | |
| <dependencies> | |
| <dependency> | |
| <groupId>junit</groupId> | |
| <artifactId>junit</artifactId> | |
| <version>4.11</version> | |
| <scope>test</scope> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.hibernate</groupId> | |
| <artifactId>hibernate-core</artifactId> | |
| <version>4.3.8.Final</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.h2database</groupId> | |
| <artifactId>h2</artifactId> | |
| <version>1.4.185</version> | |
| </dependency> | |
| </dependencies> | |
| </project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment