package org.whkt.hibernate;import junit.framework.TestCase;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.whkt.entity.Author;
public class Test extends TestCase{ Session session = null;
protected void setUp()
{
try
{
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
}
protected void tearDown()
{
try
{
session.close();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
}
public void testInsertAuthorities()
{
Transaction tx = null;
try
{
tx = session.beginTransaction();
Author user = new Author();
user.setUsername("bingxue");
user.setPassword("iloveyou");
session.save(user);
session.flush();
tx.commit();
}
catch (Exception ex)
{
System.out.println("insertAuthorities() has error!" + ex.getMessage());
tx.rollback();
}
}
}上面是测试写的一个Test类
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import org.whkt.entity.Author;
public class Test extends TestCase{ Session session = null;
protected void setUp()
{
try
{
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
}
protected void tearDown()
{
try
{
session.close();
}
catch(HibernateException ex)
{
ex.printStackTrace();
}
}
public void testInsertAuthorities()
{
Transaction tx = null;
try
{
tx = session.beginTransaction();
Author user = new Author();
user.setUsername("bingxue");
user.setPassword("iloveyou");
session.save(user);
session.flush();
tx.commit();
}
catch (Exception ex)
{
System.out.println("insertAuthorities() has error!" + ex.getMessage());
tx.rollback();
}
}
}上面是测试写的一个Test类
解决方案 »
- UCS2编码转换成GBK
- java.lang.ClassNotFoundException: org.apache.log4j.Category
- 大家给看看 是什么错误
- 怎样才能让程序重复的按时执行
- 请问如何让自己做的网站能与银行的财务系统打教导?
- sqlserver jdbc 的问题!请进来看看!
- 我的程序报这样的错,请问该怎么解决啊?
- 有谁做过这样的事情?在一类相似的实体bean上面封装一个抽象类
- 用J2EE做网上编程系统,高手进,高分!!
- 实现数据库中记录的删除操作,用CMP ENTITY EJB如何实现?
- J2EE琐碎问题,说句话就有分
- 把jsp放到WEB-INF下后引用不同目录的文件,怎么也找不到,大家帮忙!
* Created Tue Feb 21 00:10:34 CST 2006 by MyEclipse Hibernate Tool.
*/
package org.whkt.entity;import java.io.Serializable;/**
* A class that represents a row in the 'author' table.
* This class may be customized as it is never re-generated
* after being created.
*/
public class Author
extends AbstractAuthor
implements Serializable
{
/**
* Simple constructor of Author instances.
*/ public Author()
{
} /* Add customized code below */}
/*
* WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
* by MyEclipse Hibernate tool integration.
*
* Created Tue Feb 21 00:10:34 CST 2006 by MyEclipse Hibernate Tool.
*/
package org.whkt.entity;import java.io.Serializable;/**
* A class that represents a row in the author table.
* You can customize the behavior of this class by editing the class, {@link Author()}.
* WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized
* by MyEclipse Hibernate tool integration.
*/
public abstract class AbstractAuthor
implements Serializable
{ /** The value of the simple id property. */
private java.lang.Long id; /** The value of the simple username property. */
private java.lang.String username; /** The value of the simple password property. */
private java.lang.String password; /**
* Simple constructor of AbstractAuthor instances.
*/
public AbstractAuthor()
{
} /**
* Return the value of the id column.
* @return java.lang.Long
*/
public java.lang.Long getId()
{
return this.id;
} /**
* Set the value of the id column.
* @param id
*/
public void setId(java.lang.Long id)
{
this.id = id;
} /**
* Return the value of the username column.
* @return java.lang.String
*/
public java.lang.String getUsername()
{
return this.username;
} /**
* Set the value of the username column.
* @param username
*/
public void setUsername(java.lang.String username)
{
this.username = username;
} /**
* Return the value of the password column.
* @return java.lang.String
*/
public java.lang.String getPassword()
{
return this.password;
} /**
* Set the value of the password column.
* @param password
*/
public void setPassword(java.lang.String password)
{
this.password = password;
}
}
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Tue Feb 21 00:10:32 CST 2006 -->
<hibernate-mapping package="org.whkt.entity"> <class name="Author" table="author"> <id name="id" type="long">
<generator class="identity"/>
</id>
<component name="author" class="org.whkt.entity.Author">
<property name="username" column="username" type="string" />
<property name="password" column="password" type="string" />
</component>
</class>
</hibernate-mapping>
上面是实体的映射关系和实体 可能比较多问题
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration><session-factory>
<property name="myeclipse.connection.profile">conn1</property>
<property name="connection.url">
jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=whkt
</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<property name="show_sql">true</property>
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property><mapping resource="org/whkt/entity/Author.hbm.xml"></mapping>
</session-factory></hibernate-configuration>上面是Hibernate的配置文件
你要对auther属性加上getAuther(),setAuther()方法
</component>这两句应该去掉吧?