Skip to content
xamry edited this page Mar 26, 2013 · 1 revision

(Applicable for release 2.0.5 onwards)

Pickr (rhymes with Flickr, just a random fancy name we chose for picture sharing application) is an example demonstrating both transitive persistence and cross-datastore persistence feature of Kundera.

Here we'll have 3 entities. (a) Photographer who have many (b) Albums that in turn have many (c) Photos. We shall store photographer detail in MySQL, Album details in Cassandra and Photo details in HBase. You are free to choose any order and combination of database that Kundera supports.

              (1-M)                  (1-M)
Photographer --------->    Album   --------->   Photo
(MySQL)                 (Cassandra)            (HBase)

Here is step-by-step procedure for running this example. For source code and Junit test case, see the Code menu option.

Entity Classes:

Photographer Entity:

@Entity
@Table(name = "PHOTOGRAPHER", schema = "Pickr")
public class Photographer
{
    @Id
    @Column(name = "PHOTOGRAPHER_ID")
    private String photographerId;
    
    @Column(name = "PHOTOGRAPHER_NAME")
    private String photographerName;

    // Will be persisted locally
    @Embedded
    private PersonalData personalData;

    // One to many, will be persisted separately
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name="PHOTOGRAPHER_ID")
    private List<Album> albums;

    //Constructors and getter/ setters omitted 
}

PersonalData Embedded Object:

@Embeddable
public class PersonalData
{
    @Column(name = "p_website")
    private String website;

    @Column(name = "p_email")
    private String email;

    @Column(name = "p_yahoo_id")
    private String yahooId;

    //Constructors and getter/ setters omitted 
}

Album Entity:

@Entity
@Table(name = "ALBUMS", schema = "Pickr@piccandra")
public class Album
{
    @Id
    @Column(name="ALBUM_ID")
    private String albumId;

    @Column(name = "ALBUM_NAME")
    private String albumName;

    @Column(name = "ALBUM_DESC")
    private String albumDescription;

    // One to many, will be persisted separately
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinColumn(name="ALBUM_ID")
    private List<Photo> photos;

    //Constructors and getter/ setters omitted 
}

Photo Entity:

@Entity
@Table(name = "PHOTOS", schema = "Pickr@picbase")
public class Photo
{
    @Id
    @Column(name="PHOTO_ID")
    private String photoId;

    @Column(name = "PHOTO_CAPTION")
    private String photoCaption;

    @Column(name = "PHOTO_DESC")
    private String photoDescription;

    //Constructors and getter/ setters omitted 
}
    

Configuration and Server startup:

persistence.xml:

(make sure to put this file under META-INF folder in your classpath)

<persistence 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_2_0.xsd"
	version="2.0">	
	<persistence-unit name="picmysql">		
		<provider>com.impetus.kundera.KunderaPersistence</provider>		
		<class>com.impetus.kundera.examples.crossdatastore.pickr.entities.Photographer</class>
                <properties>
			<property name="kundera.client.lookup.class" value="com.impetus.client.rdbms.RDBMSClientFactory" />
                        <!--<property name="hibernate.hbm2ddl.auto" value="create" />-->
  			<property name="hibernate.show_sql" value="true" />
			<property name="hibernate.format_sql" value="true" />
			<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
			<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
			<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/Pickr" />
			<property name="hibernate.connection.username" value="root" />
			<property name="hibernate.connection.password" value="password" />
			<property name="hibernate.current_session_context_class"
				value="org.hibernate.context.ThreadLocalSessionContext" />
		</properties>
	</persistence-unit>	
	<persistence-unit name="piccandra">
		<provider>com.impetus.kundera.KunderaPersistence</provider>		
		<properties>			
			<property name="kundera.nodes" value="localhost"/>
			<property name="kundera.port" value="9160"/>
			<property name="kundera.keyspace" value="Pickr"/>
			<property name="kundera.dialect" value="cassandra"/>
			<property name="kundera.client.lookup.class" value="com.impetus.client.cassandra.pelops.PelopsClientFactory" />
			<property name="kundera.cache.provider.class" value="com.impetus.kundera.cache.ehcache.EhCacheProvider"/>
            <property name="kundera.cache.config.resource" value="/ehcache-test.xml"/>   				
		</properties>		
	</persistence-unit>
	<persistence-unit name="picbase">
		<provider>com.impetus.kundera.KunderaPersistence</provider>		
		<properties>
			<property name="kundera.nodes" value="localhost"/>
			<property name="kundera.port" value="9160"/>
			<property name="kundera.keyspace" value="Pickr"/>
			<property name="kundera.dialect" value="hbase"/>
			<property name="kundera.client.lookup.class" value="com.impetus.client.hbase.HBaseClientFactory" />
			<property name="kundera.cache.provider.class" value="com.impetus.kundera.cache.ehcache.EhCacheProvider"/>
            <property name="kundera.cache.config.resource" value="/ehcache-test.xml"/>   	
		</properties>
	</persistence-unit>
        <persistence-unit name="picongo">
		<provider>com.impetus.kundera.KunderaPersistence</provider>		
		<properties>
			<property name="kundera.nodes" value="localhost"/>
			<property name="kundera.port" value="27017"/>
			<property name="kundera.keyspace" value="Pickr"/>
			<property name="kundera.dialect" value="mongodb"/>
			<property name="kundera.client.lookup.class" value="com.impetus.client.mongodb.MongoDBClientFactory" />
			<property name="kundera.cache.provider.class" value="com.impetus.kundera.cache.ehcache.EhCacheProvider"/>
            <property name="kundera.cache.config.resource" value="/ehcache-test.xml"/>   	
		</properties>
	</persistence-unit>		
</persistence>

Server Startup:

Make sure you have MySQL, Cassandra and HBase up and running on your machine as per settings specified in persistence.xml. Also create schema and tables in corresponding database.

Saving and Retrieving data:

public static void main(String[] args) {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("picmysql,piccandra,picbase");		
	
    Photographer p = new Photographer();
    p.setPhotographerId("1");
    p.setPhotographerName("Amresh");
    p.setPersonalData(new PersonalData("www.amresh.com", "[email protected]", "xamry"));
    
    Album album1 = new Album("album_1", "My Phuket Vacation", "Went Phuket with friends"); 
    album1.addPhoto(new Photo("photo_1", "One beach", "On beach with friends"));
    album1.addPhoto(new Photo("photo_2", "In Hotel", "Chilling out in room"));
    album1.addPhoto(new Photo("photo_3", "At Airport", "So tired"));
    
    
    Album album2 = new Album("album_2", "Office Pics", "Annual office party photos");
    album2.addPhoto(new Photo("photo_4", "Office Team event", "Shot at Fun park"));
    album2.addPhoto(new Photo("photo_5", "My Team", "My team is the best"));
            
    p.addAlbum(album1);
    p.addAlbum(album2);
    
    //Save Photographer, albums, and photos
    EntityManager em = emf.createEntityManager();
    em.persist(p);
    em.close();
    
    //Find Photographer, albums, and photos
    em = emf.createEntityManager();
    Photographer pp = em.find(Photographer.class, "1");
    em.close();
    System.out.println(pp);
    
    //Run JPA Query
    em = emf.createEntityManager();
    Query q = em.createQuery("select p from Photographer p");
    List<Photographer> photographers = q.getResultList();
    em.close();
    System.out.println(photographers);
    
}

Home

Clone this wiki locally