/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.annotations.derivedidentities.e1.a;
import java.util.List;
import org.junit.Test;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.test.util.SchemaUtil;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* @author Emmanuel Bernard
*/
public class DerivedIdentitySimpleParentIdClassDepTest extends BaseCoreFunctionalTestCase {
@Test
public void testManyToOne() throws Exception {
assertTrue( SchemaUtil.isColumnPresent( "Dependent", "emp_empId", configuration() ) );
assertTrue( ! SchemaUtil.isColumnPresent( "Dependent", "emp", configuration() ) );
Session s = openSession();
s.getTransaction().begin();
Employee e = new Employee( 1L, "Emmanuel", "Manu" );
Dependent d = new Dependent( "Doggy", e );
s.persist( d );
s.persist( e );
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
DependentId dId = new DependentId( d.getName(), d.getEmp().empId );
d = (Dependent) s.get( Dependent.class, dId );
assertEquals( e.empId, d.getEmp().empId );
assertEquals( e.empName, d.getEmp().empName );
assertEquals( e.nickname, d.getEmp().nickname );
s.delete( d );
s.delete( d.getEmp() );
s.getTransaction().commit();
s.close();
}
@Test
public void testQueryNewEntityInPC() throws Exception {
Session s = openSession();
s.getTransaction().begin();
Employee e = new Employee( 1L, "Paula", "P" );
Dependent d = new Dependent( "LittleP", e );
d.setEmp(e);
s.persist( d );
s.persist( e );
// find the entity added above
Query query = s.createQuery("Select d from Dependent d where d.name='LittleP' and d.emp.empName='Paula'");
List depList = query.list();
assertEquals( 1, depList.size() );
Object newDependent = (Dependent) depList.get(0);
assertSame( d, newDependent );
s.getTransaction().rollback();
s.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Dependent.class,
Employee.class
};
}
}