I made a small crud app using struts2,spring,hibernate with oracle db. action class add methos is this.
public String addorgs() throws Exception {
t.setAO_CreatedBy("me");
t.setAO_Name("ok");
orgdao.addOrg(t);
// emplist = empdao.showEmployuee();
return SUCCESS;
}
Entity class for test:
@Entity
@Table(name="test")
public class Test {
@Id
@Column(name = "AO_ID")
@SequenceGenerator(name = "SequenceIdGenerator", sequenceName = "SEQ_AT_ORGANISATION",allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SequenceIdGenerator")
private int AO_ID;
@Column(name = "AO_Name")
private String AO_Name;
@Column(name = "AO_CreatedBy")
private String AO_CreatedBy;
public int getAO_ID() {
return AO_ID;
}
public void setAO_ID(int aO_ID) {
AO_ID = aO_ID;
}
public String getAO_Name() {
return AO_Name;
}
public void setAO_Name(String aO_Name) {
AO_Name = aO_Name;
}
public String getAO_CreatedBy() {
return AO_CreatedBy;
}
public void setAO_CreatedBy(String aO_CreatedBy) {
AO_CreatedBy = aO_CreatedBy;
}
}
DAO method :
public void addOrg(Test t) {
//System.out.println("save : "+org);
Session session = null;
session = sessionfactory.openSession();
try {
System.out.println("before save : "+t.getAO_ID());
session.save(t);
System.out.println("after save : "+t.getAO_ID());
} catch (Exception e) {
e.printStackTrace();
}
I didn't get any errors and data is not go to db. I test this code with mysql and it works fine. I think problem is with sequence. out put is like this.
before save : 0
Hibernate:
select
SEQ_AT_ORGANISATION.nextval
from
dual
after save : 1
Can you try
public void addOrg(Test t) {
//System.out.println("save : "+org);
Session session = sessionfactory.openSession();
Transaction tx;
try {
tx = session.beginTransaction();
session.save(t);
tx.commit();
System.out.println("after save : "+t.getAO_ID());
} catch (Exception e) {
if (tx!=null) tx.rollback();
e.printStackTrace();
}finally {
session.close();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.