两个表,category和topic@Entity
public class Category {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}==========@Entity
public class Topic {
private int id;
private String title;
private Category category;
private Date createDate;
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@ManyToOne
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Id
@GeneratedValue
public int getId() {
return id;
}===============之后用一个方法,放到JUnit里面测试,@Test
public void testSave() {
Session session = sf.openSession();
session.beginTransaction();
for(int i=0; i<10; i++) {
Category c = new Category();
c.setName("c" + i);
session.save(c);
System.out.println(c.getId());
}
for(int i=0; i<10; i++) {
Category c = new Category();
c.setId(1);
Topic t = new Topic();
t.setCategory(c);
t.setTitle("t" + i);
t.setCreateDate(new Date());
session.save(t);
System.out.println(t.getId());
}就是有10个category,然后第二个循环给第一个category映射10个topic。 运行后发觉报错,检查后发现,category自动生成的id是从1 到10的但是后面topic生成的id却是从11到20接下去生成的,而没有像我所想的一样也是1到10,这里就想问怎么解决这个问题? strategy里面的方法我都试过了,但是好像都没有用,topic生成的id依然是接着category生成的11到20,求解,,谢谢
public class Category {
private int id;
private String name;
@Id
@GeneratedValue
public int getId() {
return id;
}==========@Entity
public class Topic {
private int id;
private String title;
private Category category;
private Date createDate;
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
@ManyToOne
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Id
@GeneratedValue
public int getId() {
return id;
}===============之后用一个方法,放到JUnit里面测试,@Test
public void testSave() {
Session session = sf.openSession();
session.beginTransaction();
for(int i=0; i<10; i++) {
Category c = new Category();
c.setName("c" + i);
session.save(c);
System.out.println(c.getId());
}
for(int i=0; i<10; i++) {
Category c = new Category();
c.setId(1);
Topic t = new Topic();
t.setCategory(c);
t.setTitle("t" + i);
t.setCreateDate(new Date());
session.save(t);
System.out.println(t.getId());
}就是有10个category,然后第二个循环给第一个category映射10个topic。 运行后发觉报错,检查后发现,category自动生成的id是从1 到10的但是后面topic生成的id却是从11到20接下去生成的,而没有像我所想的一样也是1到10,这里就想问怎么解决这个问题? strategy里面的方法我都试过了,但是好像都没有用,topic生成的id依然是接着category生成的11到20,求解,,谢谢
解决方案 »
- javac不是内部或外部命令
- 寻JSP视频教程!!谢谢!!
- xfire webservice 客户端调用服务端 一个小问题
- 高手解决weblogic的配置 JDBC 连接缓冲池问题
- 如何能调试ejb?
- ejb简单问题,javax.naming.NoInitialContextException,可我代码已经new InitialContext()了
- apache+tomcat+mysql测试的问题
- 请问哪儿有比较好的jdbc3.0下载,我现在用jdbc2.0不支持rs.absolute()等方法。
- 紧急求助:希望大侠能帮帮忙(关于java调用.dll文件的jni 问题)
- (初学者)EJB远程调用中的错误-急急急!
- 在jsp中判断ArrayLis中存不存在一个值的方法
- 关于jsp中获取错误码的问题request.getAttribute("javax.servlet.error.status_code")
解决办法:
@SequenceGenerator(name="topic_seq", sequenceName="topic_seq",allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="topic_seq")