Hi Guys,
I’m recently a small project that required me to put my attention to the RDBMS after some long times ago develop application with Graph Database.
Playing again with it was like dejavu to me. OK, without any further a do so just get hand on to the all tools that is mentioned in the subject.
Today, I am going to put the step by step of using Vaadin with the combination of Springdata to access MySQL. Since I don’t like to get my hands dirty to create database table directly so that I use ORM as the bridge between my object and database. Here I use hibernate to manage the all query things and database table creation.
Let start the step below.
1. Setting up your environment (Jboss AS, Eclipse and Etc)
2. Create Vaadin project from maven
3. Follow the following dependencies to POM.xml. Please ensure that you are using hibernate 4 and the latest sporingdata because the Hbncontainer is only suit with these requirement.
<properties></pre> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <vaadin.version>6.7.1</vaadin.version> <gwt.version>2.3.0</gwt.version> <gwt.plugin.version>2.2.0</gwt.plugin.version> </properties> <dependencies> <dependency> <groupId>com.vaadin</groupId> <artifactId>vaadin</artifactId> <version>${vaadin.version}</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-jpa</artifactId> <version>1.1.0.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.20</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.1.1.RELEASE</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.1.1.Final</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.vaadin.addons</groupId> <artifactId>hbncontainer</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.6.6</version> </dependency> </dependencies> <pre>
4. Create the datasource-bean.xml in the application classpath as highlighted below
5. Here is the datasource-bean.xml looks like
<?xml version="1.0" encoding="UTF-8"?></pre> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="sessionFactoryhibernate" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="mySqlDataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.current_session_context_class">thread</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="annotatedClasses"> <list> <value>study.mehibernate.Biodata</value> </list> </property> </bean> <bean id="mySqlDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/myappllication" /> <property name="username" value="root" /> <property name="password" value="***" /> </bean> <bean id="tradeDAO" class="study.mehibernate.HibernateDAO"> <property name="sessionFactory" ref="sessionFactoryhibernate" /> </bean> </beans>
6. Create the hibernate helper. This hibernate helper will manage the session management
package study.mehibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; public class HibernateDAO { SessionFactory sessionFactory = null; Session session = null; public SessionFactory getFactory() { return sessionFactory; } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public Session getSession() { return session; } public void setSession(Session session) { this.session = session; } }
7. Create the spring context helper
package study.mehibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.orm.hibernate3.HibernateTransactionManager; public class ContextHelper { public static ApplicationContext ctx = null; public static Session session = null; private static HibernateDAO dao = null; public static ApplicationContext getCtx() { if(ctx == null){ ctx = new ClassPathXmlApplicationContext("datasource-bean.xml"); } return ctx; } public static Session getSession(){ if(ctx == null){ getCtx(); } if(dao == null){ dao = ctx.getBean("tradeDAO", HibernateDAO.class); } if(session == null){ session = dao.getSessionFactory().openSession(); } if(!session.getTransaction().isActive()){ session.beginTransaction(); } return session; } public static void InitHibernate(){ if(ctx == null){ getCtx(); } if(dao == null){ dao = ctx.getBean("tradeDAO", HibernateDAO.class); } } }
8. Don’t forget to implement the vaadin hibernate util session manager
</pre> package study.mehibernate; import org.hibernate.Session; import com.vaadin.data.hbnutil.HbnContainer.SessionManager; public class DataSource implements SessionManager { public Session getSession() { // TODO Auto-generated method stub return ContextHelper.getSession(); } }
9. Create the Hibernate entity, I use a very simple one to become example
</pre> package study.mehibernate; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Biodata { int id; String name; int age; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, nullable = false) public int getId() { return id; } public void setId(int id) { this.id = id; } @Column(name = "name", unique = false, nullable = false) public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name = "age", unique = false, nullable = false) public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
10. Finaly for the preparation above is all complete, so we can now create the application. Follow my example
</pre> package study.mehibernate; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.vaadin.Application; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; import com.vaadin.ui.Label; import com.vaadin.ui.Window; /** * The Application's "main" class */ @SuppressWarnings("serial") public class MyVaadinApplication extends Application { private Window window; @Override public void init() { ContextHelper.InitHibernate(); window = new MyWindow(); setMainWindow(window); } }
11. Create my own window
package study.mehibernate; import java.util.Collection; import java.util.List; import org.hibernate.Session; import com.vaadin.data.hbnutil.HbnContainer; import com.vaadin.data.hbnutil.HbnContainer.SessionManager; import com.vaadin.ui.ComboBox; import com.vaadin.ui.Table; import com.vaadin.ui.Window; public class MyWindow extends Window { Session session; Table table; HbnContainer<Biodata> container; List<Biodata> temp; ComboBox combo; public MyWindow() { // TODO Auto-generated constructor stub session = ContextHelper.getSession(); table = new Table(); container = new HbnContainer<Biodata>(Biodata.class, new DataSource()); table.setContainerDataSource(container); table.setVisibleColumns(new String[] {"name", "age"}); addComponent(table); combo = new ComboBox(); combo.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_EXPLICIT); combo.setItemCaptionPropertyId("name"); combo.setContainerDataSource(container); addComponent(combo); //initTable(); } public void initTable(){ session.beginTransaction(); Biodata bio1 = new Biodata(); bio1.setName("Rio"); bio1.setAge(29); Biodata bio2 = new Biodata(); bio2.setName("Annis"); bio2.setAge(27); Biodata bio3 = new Biodata(); bio3.setName("Daanish"); bio3.setAge(2); session.save(bio1); session.save(bio2); session.save(bio3); session.getTransaction().commit(); } }
Yes this is it all the code that you need .. and the result of the application is just like the picture below
Nice post! inspires me to convert to Java and graph db stuff
Hi Radith,
Good… Nice to hear some comments from you. If you have any further question just drop me an email on rio.asmara@gmail.com Thanks
private static ApplicationContext getCtx() in this method i’m getting error like that “Unhandled method”. If i add try/catch, then gettin another error “BeansException” not throwable.
How can i fix it?
Thank u in advance
hello plz help me how database connect in Vaadin framework me using mysql databse server