Tags

, ,


Create a simple class , which should be mapped to database table. We will be using ManyToOne and OneToOne mapping in account.

import java.io.Serializable;
import javax.persistence.Entity;

@Entity
public class MyTable implements Serializable
{
  private Integer id;
  private String title;
  private Artist artist;
  private String label;
  private Date releaseDate;
  private List<Track> tracks;










 @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  public Integer getId()
  {
    return id;
  }










@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE })
  public Artist getArtist()
  {
    return artist;
  }










 @OneToMany(cascade=CascadeType.ALL,
             fetch=FetchType.EAGER)
  public List<Track>getTracks()
  {
    return tracks;
  }










 @ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
  public Record getRecord()
  {
    return record;
  }

}

2nd class-table mapping of above used class Track

import java.io.Serializable;
import javax.persistence.Entity;

@Entity
public class Track implements Serializable
{
  private Integer id;
  private String title;
  private int lengthInSeconds;
  private Record record;
  /*
   .... standard getters/setters/equals/hashCode/toString
  */
}

Finally, here’s the Artist:

import java.io.Serializable;
import javax.persistence.Entity;

@Entity
public class Artist implements Serializable
{
  private Integer id;
  private String name;
  private String genre;
  /*
   .... standard getters/setters/equals/hashCode/toString
  */
}



Now Using a Hibernate support and Java code to call a DAO classes and its integration with Hibernate session

/**
 * Implementation of MusicDAO implemented by extending Spring's HibernateDaoSupport
 * @author roblambert
 */
public class MusicDAOHibernateImpl extends HibernateDaoSupport implements MusicDAO
{
  public Collection<Artist> getArtists()
  {
    return getHibernateTemplate().loadAll(Artist.class);
  }

  public Artist getArtistById(Integer id)
  {
    Artist artist = (Artist) getHibernateTemplate().load(Artist.class, id);
    return artist;
  }

  public Artist saveArtist(Artist artist)
  {
    getHibernateTemplate().saveOrUpdate(artist);
    return artist;
  }

  public Record getRecordById(Integer id)
  {
    Record record = (Record) getHibernateTemplate().load(Record.class, id);
    return record;
  }

  public Record saveRecord(Record record)
  {
    getHibernateTemplate().saveOrUpdate(record);
    return record;
  } 

  public Track getTrackById(Integer id)
  {
    Track track = (Track) getHibernateTemplate().load(Track.class, id);
    return track;
  }

  public List<Record> searchRecordsByTitle(String title)
  {
    return getHibernateTemplate().findByNamedQuery("record.titleLike", "%"+title+"%");
  }
}

Now we will use Spring’s AnnotationSessionFactoryBean to use annotated class. This is what gives us support for making the system aware the of mappings for persistence using the annotations.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="hibernateProperties">

      <props>
<!--YOUR HIBERNATE PROPERTIES HERE...-->
      </props>
    </property>
    <property name="annotatedClasses">
      <list>

        <value>com.zabada.springrecipes.hibernatejpa.entity.Track</value>
        <value>com.zabada.springrecipes.hibernatejpa.entity.Record</value>
        <value>com.zabada.springrecipes.hibernatejpa.entity.Artist</value>
      </list>

    </property>
  </bean>

  <!--sessionFactory will get autowired-->
  <bean id="hibernateInterceptor"
        class="org.springframework.orm.hibernate3.HibernateInterceptor"
        autowire="byName" />

  <!--sessionFactory will get autowired-->

  <bean id="musicDaoTarget"
        class="com.zabada.springrecipes.hibernatejpa.dao.MusicDAOHibernateImpl"
        autowire="byName" />

  <bean id="musicDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
      <value>com.zabada.springrecipes.hibernatejpa.dao.MusicDAO</value>
    </property>

    <property name="interceptorNames">
      <list>
        <value>hibernateInterceptor</value>
        <value>musicDaoTarget</value>
      </list>

    </property>
  </bean>

</beans>


Now one another example of named query :

@Entity
@NamedQuery(name="record.titleLike",
            query="select r from Record r where r.title like ?")
public class Record implements Serializable
{
  //...
}
Advertisements