HHH-4665 add tests for getIdentifier()

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18302 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Emmanuel Bernard 2009-12-21 15:49:55 +00:00
parent 54706c4b20
commit 23c1a47667
4 changed files with 271 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package org.hibernate.ejb.test.util;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
/**
* @author Emmanuel Bernard
*/
@Entity
public class Book {
private Long id;
private String name;
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,62 @@
package org.hibernate.ejb.test.util;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import org.hibernate.ejb.test.TestCase;
/**
* @author Emmanuel Bernard
*/
public class GetIdentifierTest extends TestCase {
public void testSimpleId() {
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Book book = new Book();
em.persist( book );
em.flush();
assertEquals( book.getId(), em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier( book ) );
em.getTransaction().rollback();
em.close();
}
public void testEmbeddedId() {
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Umbrella umbrella = new Umbrella();
umbrella.setId( new Umbrella.PK() );
umbrella.getId().setBrand( "Burberry" );
umbrella.getId().setModel( "Red Hat" );
em.persist( umbrella );
em.flush();
assertEquals( umbrella.getId(), em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier( umbrella ) );
em.getTransaction().rollback();
em.close();
}
public void testIdClass() {
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Sickness sick = new Sickness();
sick.setClassification( "H1N1" );
sick.setType("Flu");
em.persist( sick );
em.flush();
Sickness.PK id = new Sickness.PK();
id.setClassification( sick.getClassification() );
id.setType( sick.getType() );
assertEquals( id, em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier( sick ) );
em.getTransaction().rollback();
em.close();
}
public Class[] getAnnotatedClasses() {
return new Class[] {
Book.class,
Umbrella.class,
Sickness.class
};
}
}

View File

@ -0,0 +1,96 @@
package org.hibernate.ejb.test.util;
import java.util.Date;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Temporal;
import javax.persistence.Id;
import javax.persistence.IdClass;
/**
* @author Emmanuel Bernard
*/
@Entity
@IdClass(Sickness.PK.class)
public class Sickness {
private Date beginTime;
private String type;
private String classification;
@Id
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Id
public String getClassification() {
return classification;
}
public void setClassification(String classification) {
this.classification = classification;
}
@Temporal(javax.persistence.TemporalType.DATE)
public Date getBeginTime() {
return beginTime;
}
public void setBeginTime(Date beginTime) {
this.beginTime = beginTime;
}
public static class PK implements Serializable {
private String type;
private String classification;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getClassification() {
return classification;
}
public void setClassification(String classification) {
this.classification = classification;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
PK pk = ( PK ) o;
if ( classification != null ? !classification.equals( pk.classification ) : pk.classification != null ) {
return false;
}
if ( type != null ? !type.equals( pk.type ) : pk.type != null ) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = type != null ? type.hashCode() : 0;
result = 31 * result + ( classification != null ? classification.hashCode() : 0 );
return result;
}
}
}

View File

@ -0,0 +1,81 @@
package org.hibernate.ejb.test.util;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.EmbeddedId;
/**
* @author Emmanuel Bernard
*/
@Entity
public class Umbrella {
private PK id;
private int size;
@EmbeddedId
public PK getId() {
return id;
}
public void setId(PK id) {
this.id = id;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public static class PK implements Serializable {
private String model;
private String brand;
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
@Override
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
PK pk = ( PK ) o;
if ( brand != null ? !brand.equals( pk.brand ) : pk.brand != null ) {
return false;
}
if ( model != null ? !model.equals( pk.model ) : pk.model != null ) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = model != null ? model.hashCode() : 0;
result = 31 * result + ( brand != null ? brand.hashCode() : 0 );
return result;
}
}
}