关于annotation!希望路过的大侠指点指点!
以前一直用Xml配置类与数据表的映射,最近也了解了一下annotation,觉得挺方便的,也尝试着做了一下,但是在ID生成策略那里出了问题,我在数据库里建了一个vu_seq的sequence,从1开始,每次增加1.在实体类里也指定了具体的策略,测试的时候hibernate自动生成的sql语句也的确去找了这个sequence的下一个值的,但我断点的时候发现sequence的下一个值明明应该是1,可hibernate赋到Id的时候却变成了50,再次插入的时候就变成了100,而在数据库查出vu_seq的下一个值还是2,也就是说,hibernate在自动生成ID的时候的确是找了我创建的那个sequence,但却是从50开始了,而且每次增加的值也是50.我找了半天也找不出原因所在,在此把代码贴出来,希望各位路过的大侠帮忙看一下,感激不尽!
Oracle里的sequence:
create sequence vu_seq
start with 1
increment by 1
nomaxvalue
cache 10;
package com.accp.entity;
import java.io.Serializable;
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="vote_user")
@SequenceGenerator(name="vu_seq",sequenceName="vu_seq")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String password;
@Id
@Column(name="vu_user_id")
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="vu_seq")
public Integer getId() {
return id;
}
@Column(name="vu_user_name")
public String getName() {
return name;
}
@Column(name="vu_password")
public String getPassword() {
return password;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
}
sessionfactory:
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:Vote</property>
<property name="connection.username">accp</property>
<property name="connection.password">accp</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->
<!--<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>-->
<mapping class="com.accp.entity.User"/>
</session-factory>
测试类:
public class SaveTest extends BaseDaoHibernateImpl {
public static void main(String[] args) {
User user=new User();
user.setName("123");
user.setPassword("123");
Session session=new SaveTest().getSession();
Transaction transaction=session.beginTransaction();
try {
session.save(user);
transaction.commit();
} catch (HibernateException e) {
// TODO: handle exception
transaction.rollback();
e.printStackTrace();
}
}
希望哪位大侠能够指点一下问题到底出在哪里,小弟在此谢了!