实体类:
public class VoteOption implements java.io.Serializable {
// Fields
private Integer voId;
private String voOption;
private Integer vsId;
private Integer voOrder;
// Constructors
/** default constructor */
public VoteOption() {
}
/** minimal constructor */
public VoteOption(String voOption) {
this.voOption = voOption;
}
/** full constructor */
public VoteOption(String voOption, Integer vsId, Integer voOrder) {
this.voOption = voOption;
this.vsId = vsId;
this.voOrder = voOrder;
}
// Property accessors
public Integer getVoId() {
return this.voId;
}
public void setVoId(Integer voId) {
this.voId = voId;
}
public String getVoOption() {
return this.voOption;
}
public void setVoOption(String voOption) {
this.voOption = voOption;
}
public Integer getVsId() {
return this.vsId;
}
public void setVsId(Integer vsId) {
this.vsId = vsId;
}
public Integer getVoOrder() {
return this.voOrder;
}
public void setVoOrder(Integer voOrder) {
this.voOrder = voOrder;
}
}
和数据库映射:
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.test.entity.VoteOption" table="VOTE_OPTION" schema="dbo" catalog="vote">
<id name="voId" type="java.lang.Integer">
<column name="vo_id" />
<generator class="native" />
</id>
<property name="voOption" type="java.lang.String">
<column name="vo_option" length="100" not-null="true" />
</property>
<property name="vsId" type="java.lang.Integer">
<column name="vs_id" />
</property>
<property name="voOrder" type="java.lang.Integer">
<column name="vo_order" />
</property>
</class>
</hibernate-mapping>
执行更新操作得先查出数据在更新:
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.test.entity.VoteOption;
public class Test1 {
private static Configuration conf = null;
private static SessionFactory sessionFactory = null;
private static Session session = null;
private static Transaction tx = null;
public static void main(String[] args) {
try {
VoteOption option = new VoteOption("e");
option.getVoId();
option.getVoOrder();
option.getVsId();
conf = new Configuration().configure();
sessionFactory = conf.buildSessionFactory();
session = sessionFactory.openSession();
option = (VoteOption) session.get(option.getClass(), 8);
tx = session.beginTransaction();
option.setVoOption("zgliang");
// session.delete(option);
session.update(option);
tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
// 7、关闭session
session.close();
// 8、关闭sessionFactory工厂
sessionFactory.close();
}
}
}