日期:2014-05-20  浏览次数:20719 次

hibernate插入ORACLE自增长序列乱码问题,求助!
@Entity
@Table(name = "myuser")
@SequenceGenerator(name="payablemoney_seq", sequenceName="SEQ_USERID")
public class User {
private int id;
private String username;
private String password;
@Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE,generator="payablemoney_seq")  
  public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
}



@Component("userDao")
public class UserDaoImpl implements UserDao {
private HibernateTemplate hibernateTemplate; 
public void save(User u) {
System.out.println(u.getId()+"——"+u.getUsername()+"——"+u.getPassword());
hibernateTemplate.save(u);

}
}


自增长序列SEQ_USERID数据库中已经建好,然后执行hibernate插入的时候id值每次都是乱码???
myeclipse控制台打印的日志:
0——aaa——aaa
Hibernate: select SEQ_USERID.nextval from dual
Hibernate: insert into myuser (password, username, id) values (?, ?, ?)

select SEQ_USERID.nextval from dual这一句放到数据库中可以执行获得自增长序列值,然后插入后
就变成乱码,如下:
SQL> select SEQ_USERID.nextval from dual
  2 ;
 
  NEXTVAL
----------
1000000002
 
SQL> select * from myuser;
 
  ID USERNAME PASSWORD
----------- -------------------- --------------------
-1539607450 aaa aaa

------解决方案--------------------
@SequenceGenerator(name="payablemoney_seq", sequenceName="SEQ_USERID",allocationSize=1,initialValue=1)
试一下