java - How to save entity into database? -
i have simple spring mvc project adding users database. utilize hibernate 4.1.9, spring framework 3.2.0 , postgresql 9.2.
see code below
user bean
package org.vdzundza.beans; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.sequencegenerator; import javax.persistence.table; @entity @table(name = "users") public class user { @id @generatedvalue(strategy = generationtype.sequence, generator = "user_seq") @sequencegenerator(name = "user_seq", sequencename = "user_seq") @column(name = "id") private long id; @column(name = "network") private string network; @column(name = "photo") private string photo; @column(name = "identity") private string identity; @column(name = "firstname") private string firstname; @column(name = "lastname") private string lastname; @column(name = "hash") private string hash; @column(name = "isonline") private boolean isonline; public user() { } //getters , setters @override public string tostring(){ stringbuilder out = new stringbuilder(); out.append("["); out.append("name: " + firstname + " " + lastname); out.append("\tnetwork: " + network); out.append("\t online: " + isonline); homecoming out.tostring(); } }
userdao
package org.vdzundza.dao; import java.util.list; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.repository; import org.vdzundza.beans.user; @repository public class userdaoimpl implements userdao { @autowired private sessionfactory sessionfactory; @override public void adduser(user user) { sessionfactory.opensession().save(user); } @suppresswarnings("unchecked") @override public list<user> listuser() { homecoming sessionfactory.opensession().createcriteria(user.class).list(); } }
userservice
package org.vdzundza.service; import java.util.list; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.service; import org.springframework.transaction.annotation.transactional; import org.vdzundza.beans.user; import org.vdzundza.dao.userdao; @service public class userserviceimpl implements userservice { @autowired private userdao userdao; @transactional public void adduser(user user) { userdao.adduser(user); } @transactional public list<user> listuser() { homecoming userdao.listuser(); } }
root-context.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c" 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.1.xsd"> <!-- root context: defines shared resources visible other web components --> <context:annotation-config /> <context:component-scan base-package="org.vdzundza.dao" /> <context:component-scan base-package="org.vdzundza.service" /> <bean id="propertiesconfig" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer" p:location="/web-inf/jdbc.properties" /> <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <property name="driverclassname" value="${jdbc.driverclassname}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean" lazy-init="false"> <property name="datasource" ref="datasource" /> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.postgresqldialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <property name="packagestoscan" value="org.vdzundza.beans" /> </bean> <bean id="transactionalmanager" class="org.springframework.orm.hibernate4.hibernatetransactionmanager"> <property name="sessionfactory" ref="sessionfactory" /> </bean> </beans>
usercontroller
package org.vdzundza.web; import java.util.map; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.validation.bindingresult; import org.springframework.web.bind.annotation.modelattribute; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.requestmethod; import org.vdzundza.beans.user; import org.vdzundza.service.userservice; @controller @requestmapping("/user") public class usercontroller { @autowired private userservice userservice; @requestmapping("/") public string index(){ homecoming "redirect:/user/index"; } @requestmapping("/index") public string listusers(map<string, object> map){ map.put("user", new user()); map.put("userlist", userservice.listuser()); homecoming "user"; } @requestmapping(value = "/add",method = requestmethod.post) public string adduser(@modelattribute("user") user user, bindingresult result){ userservice.adduser(user); homecoming "redirect:/user/index"; } }
user.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>user test spring , hibernate</title> </head> <body> <a href="${pagecontext.request.contextpath}/">go back</a> <br /> <form:form method="post" action="${pagecontext.request.contextpath}/user/add" commandname="user"> <table> <tr> <td><form:label path="firstname">first name</form:label></td> <td><form:input path="firstname" /></td> </tr> <tr> <td><form:label path="lastname">last name</form:label></td> <td><form:input path="lastname" /></td> </tr> <tr> <td><form:label path="network">network</form:label></td> <td><form:input path="network" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="add user" /></td> </tr> </table> </form:form> <c:if test="${empty userlist}"> <h2>user list empty</h2> </c:if> <c:if test="${!empty userlist}"> <h2>user list</h2> <table> <tr> <th>first name</th> <th>last name</th> <th>network</th> </tr> <c:foreach items="${userlist}" var="user"> <tr> <td>${user.firstname}</td> <td>${user.lastname}</td> <td>${user.network}</td> </tr> </c:foreach> </table> </c:if> </body> </html>
new user isn't saved in database. when click "add user" see in console:
hibernate: select nextval ('user_seq') hibernate: select this_.id id0_0_, this_.firstname firstname0_0_, this_.hash hash0_0_, this_.identity identity0_0_, this_.isonline isonline0_0_, this_.lastname lastname0_0_, this_.network network0_0_, this_.photo photo0_0_ users this_
how prepare problem?
after open session , save object... flush session, close it.
session session = sessionfactory.opensession(); session.save(user); session.flush(); session.close();
java hibernate spring-mvc
No comments:
Post a Comment