首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

实在是没有办法了,便是找不到哪里出错了!求大神!

2012-08-31 
实在是没有办法了,就是找不到哪里出错了!!!求大神!!!!本人菜鸟,看张龙老师的视频学习hibernate。按照视频写

实在是没有办法了,就是找不到哪里出错了!!!求大神!!!!
本人菜鸟,看张龙老师的视频学习hibernate。 
按照视频写的代码,结果只对了一半。
我是用sql server 2005+tomcat 7.0+hibernate 4.10+struts 2.3 搭建的。

代码比较杂,还请各位耐心点。

注册页面代码,register.jsp

<form action="savePerson.action">

username:<input type="text" name="username" size="20"><br>
password:<input type="password" name="password" size="20"><br>
age:<input type="text" name="age" size="20"><br>

<input type="submit" value="submit">

</form>


struts.xml 代码

<package name="hibernate" extends="struts-default">

<action name="savePerson" class="com.shengsiyuan.action.PsersonAction" method="savePerson">

<result name="success">/result.jsp</result>

</action>


PsersonAction.java 代码

package com.shengsiyuan.action;

import com.opensymphony.xwork2.ActionSupport;
import com.shengsiyuan.modle.Person;
import com.shengsiyuan.service.PersonService;
import com.shengsiyuan.service.PersonServiceImpl;

public class PsersonAction extends ActionSupport {

/**

*/
private static final long serialVersionUID = 1L;

private String username;

private String password;

private int age;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String savePerson() throws Exception{

Person person=new Person();

person.setUsername(username);
person.setPassword(password);
person.setAge(age);

java.sql.Date registerDate=new java.sql.Date(new java.util.Date().getTime());

person.setRegisterDate(registerDate);

PersonService personService=new PersonServiceImpl();

personService.savePerson(person);

return SUCCESS;
}

}

Person.java 代码

package com.shengsiyuan.modle;

import java.util.Date;

public class Person {

private Integer id;

private String username;

private String password;

private Integer age;

private Date registerDate;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public Date getRegisterDate() {
return registerDate;
}

public void setRegisterDate(Date registerDate) {
this.registerDate = registerDate;
}

PersonServiceImpl.java 代码

package com.shengsiyuan.service;

import com.shengsiyuan.dao.PersonDAO;
import com.shengsiyuan.modle.Person;
import com.shengsiyuan.dao.PersonDAOImpl;

public class PersonServiceImpl implements PersonService
{
@Override
public void savePerson(Person person) 
{
PersonDAO personDAO=new PersonDAOImpl();

personDAO.savePerson(person);
}


}


PersonDAOImpl.java 代码

package com.shengsiyuan.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;


import com.shengsiyuan.modle.Person;
import com.shengsiyuan.util.HibernateUtil;

import com.shengsiyuan.dao.PersonDAO;

public class PersonDAOImpl implements PersonDAO
{
@Override
public void savePerson(Person person) 
{
Session session=HibernateUtil.openSession();
Transaction tx=session.beginTransaction();

try
{
session.save(person);
}
catch(Exception ex)
{
if(null!=tx)
{
tx.rollback();
}
}
finally
{
HibernateUtil.close(session);
}

}

}

HibernateUtil.java 代码

package com.shengsiyuan.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

@SuppressWarnings("deprecation")
public class HibernateUtil 
{
private static SessionFactory sessionFactory;

static
{
try
{
sessionFactory=new Configuration().configure().buildSessionFactory();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}

public static Session openSession()
{
Session session =sessionFactory.openSession();

return session;
}

public static void close(Session session)
{
if(null!=session)
{
session.close();
}
}
}


服务器端输出如下代码


八月 09, 2012 3:13:20 下午 org.apache.catalina.startup.SetContextPropertiesRule begin
警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:webDemo' did not find a matching property.
八月 09, 2012 3:13:20 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-bio-8080"]
八月 09, 2012 3:13:20 下午 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-bio-8009"]
八月 09, 2012 3:13:20 下午 org.apache.catalina.startup.Catalina start
信息: Server startup in 2807 ms
八月 09, 2012 3:13:31 下午 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
八月 09, 2012 3:13:31 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.0.Final}
八月 09, 2012 3:13:31 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
八月 09, 2012 3:13:31 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
八月 09, 2012 3:13:31 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
八月 09, 2012 3:13:31 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
八月 09, 2012 3:13:31 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: Person.hbm.xml
八月 09, 2012 3:13:33 下午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
八月 09, 2012 3:13:34 下午 org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator c3p0ProviderPresent
WARN: HHH000022: c3p0 properties were encountered, but the org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider provider class was not found on the classpath; these properties are going to be ignored.
八月 09, 2012 3:13:34 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
八月 09, 2012 3:13:34 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure


INFO: HHH000115: Hibernate connection pool size: 20
八月 09, 2012 3:13:34 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
八月 09, 2012 3:13:34 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [com.microsoft.sqlserver.jdbc.SQLServerDriver] at URL [jdbc:sqlserver://localhost:1433;DatabaseName=hibernate]
八月 09, 2012 3:13:34 下午 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=sa, password=****}
八月 09, 2012 3:13:34 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
八月 09, 2012 3:13:34 下午 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
八月 09, 2012 3:13:34 下午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
八月 09, 2012 3:13:34 下午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select max(id) from person


少了插入语句,数据库了没有数据



[解决办法]
事务好像没有提交吧
[解决办法]
断点调试下 看看在哪部出问题了、
首先看看 person这个对象里面的属性为空吗?
然后具体看看执行到哪个方法出现异常、
[解决办法]
构造方法没有,事务没有commit。
[解决办法]
在session.save(object)后调用transaction.commit()!!
[解决办法]
Transaction tx=session.beginTransaction();

try
{
session.save(person);
}
catch(Exception ex)
{
if(null!=tx)
{
tx.rollback();
}
}
finally
{
HibernateUtil.close(session);
}

首先你开启了事务但是在save的时候你没有提交事务,所以不能正常存储数据。事务是默认自动提交的,当你手动开始事务的时候要记得提交事务。
[解决办法]
建议你在学习hibernate之前先学习一下jdbc,这样能帮助你对框架的理解

热点排行