Code Related

Links

I have found these links very helpful.

 

Mapping Class Inheritance

Mapping Relationships

Hibernate, JPA, with Spring Transactions

This tutorial will describe the complete process of setting up Hibernate to work with the new JPA with Spring transaction support.

Table Setup

--
-- Table structure for table `city`
--

CREATE TABLE IF NOT EXISTS `city` (
`city_id` smallint(6) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`state_id` smallint(6) DEFAULT NULL,
`short_name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`city_id`),
KEY `state_id` (`state_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;

CREATE TABLE IF NOT EXISTS `country` (
`country_id` smallint(6) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`short_name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`country_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 


CREATE TABLE IF NOT EXISTS `state` (
`state_id` smallint(6) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`short_name` varchar(20) DEFAULT NULL,
`country_id` smallint(6) DEFAULT NULL,
PRIMARY KEY (`state_id`),
KEY `country_id` (`country_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Mapping the relationships

Mapping Class Inheritance

Table per class hierarchy

People and Organizations are types that are stored in the party table. We use the following mapping to define relationship. The party class is defined to be abstract and the Person and Organization subclass from it.

@Entity
@Table(name = "party")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "party_type_code", discriminatorType = DiscriminatorType.INTEGER)
public abstract class Party {
@Id
@Column(name="party_id")
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@Column(name="comment")
private String comment;
@OneToMany(mappedBy="party")
private Set<PartyAddress> partyAddress = new HashSet<PartyAddress>();
...

}

@Entity
@DiscriminatorValue("1")
public class Organization extends Party {
private String name;

@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

@Entity
@DiscriminatorValue("2")
public class Person extends Party {
private String lastName;
private String firstName;
private String middleName;
private String personnalTitle;
private String suffix;
private String nickname;
private Boolean isMale;
private Integer heightInInches;
private String mothersMaidenName;
private Boolean isMaried;
private String ssn;
private String passportNumber;
private Date passportExpireDate;
private Integer totalYearsWorkExp;
private Date birthdate;

}

Mapping Relationships

Lets setup an JPA project

Within the META-INF folder you need to have the following file.

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="Artistic">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>

Spring Configuration file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<import resource="spring-infrastructure.xml" />

<bean name="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- post-processors for all standard config annotations -->
<context:annotation-config />

<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean name="orgPeopleDataManager" class="orgpeople.OrgPeopleDataManagerImpl">
<property name="emf" ref="entityManagerFactory" />
</bean>
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
<property name="jpaDialect" ref="hibernateDialect"></property>
</bean>
<bean name="hibernateDialect"
class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

<tx:annotation-driven transaction-manager="txManager" />

</beans>

 

Java Code that actually does the work

@Override
@Transactional
public void addCityToState(City city, State state) {
EntityManager em = emf.createEntityManager();
em.joinTransaction();
State stateBean = em.find(State.class, state.getId());
if(stateBean==null) { throw new NullPointerException("state was not found."); }
city.setState(stateBean);
stateBean.getCities().add(city);
}

Resulted in the following sql being executed

2513 [main] DEBUG org.hibernate.SQL - insert into city (city_id, name, short_name, state_id) values (null, ?, ?, ?)

 

more to come...

vermatech homepage