Reputation: 1969
I am using hibernate 3 and spring 3.0.7, I am trying to get my code to save an entity to the database but it just won't do it.
I have tried everything with it but it seems like there is something I am doing wrong.
here is my class that uses the dao method
public boolean saveData(DataHolder holder,int type,Owners found){
TempData temp = new TempData();
temp.setDate(holder.getDate());
temp.setTimestamp(new Timestamp(holder.getDate().getTime()));
temp.setName(holder.getName());
temp.setComId(holder.getCom().getComId());
temp.setSymbol(holder.getCom().getSymbol());
temp.setPercent(holder.getPercent());
if(type == 1){
temp.setOwnerId(found.getOwnerId());
temp.setOwnerType(found.getType());
tempDao.addTemp(temp);
return true;
}
else{
tempDao.addTemp(temp);
return false;
}
}
ofcourse it's a bean annotated with component
and here is my dao's add method thats not working
public boolean addTemp(TempData entity){ try { getSession().save(entity); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
and this is my entity
@Component public class TempData {
private int tempId;
private Date date;
private Timestamp timestamp;
private String name;
private String ownerType;
private Integer ownerId;
private String symbol;
private Integer comId;
private Double percent;
public int getTempId() {
return tempId;
}
public void setTempId(int tempId) {
this.tempId = tempId;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public Timestamp getTimestamp() {
return timestamp;
}
public void setTimestamp(Timestamp timestamp) {
this.timestamp = timestamp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOwnerType() {
return ownerType;
}
public void setOwnerType(String ownerType) {
this.ownerType = ownerType;
}
public Integer getOwnerId() {
return ownerId;
}
public void setOwnerId(Integer ownerId) {
this.ownerId = ownerId;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
public Integer getComId() {
return comId;
}
public void setComId(Integer comId) {
this.comId = comId;
}
public Double getPercent() {
return percent;
}
public void setPercent(Double percent) {
this.percent = percent;
}
and this is my hbm
and this is spring config's part thats related
<tx:advice id="tx" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" propagation="REQUIRES_NEW"/>
<tx:method name="add*" propagation="REQUIRES_NEW"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor advice-ref="tx" pointcut="execution(* *..AbstractDao.*(..))" />
<aop:advisor advice-ref="tx" pointcut="execution(* *..TempDataDao.addTemp(..))" />
</aop:config>
thing is it retrieves data perfectly but when it comes to saving data it just don't work, and the same methods in just different projects work perfectly, but here they just don't and the log says nothing about any error or something, i even tried to change the name of the table mapped to a false one but still no errors and completes the transaction,
what am i missing here ?
EDIT
This is what my debugger shows, and just that
DEBUG [http-bio-8080-exec-7] (HibernateTransactionManager.java:569) - Exposing Hibernate transaction as JDBC transaction [jdbc:mysql://localhost:3306/parse_web, UserName=root@localhost, MySQL-AB JDBC Driver]
DEBUG [http-bio-8080-exec-7] (SessionImpl.java:265) - opened session at timestamp: 13683691714
DEBUG [http-bio-8080-exec-7] (AbstractSaveEventListener.java:134) - generated identifier: 0, using strategy: org.hibernate.id.Assigned
DEBUG [http-bio-8080-exec-7] (AbstractPlatformTransactionManager.java:752) - Initiating transaction commit
DEBUG [http-bio-8080-exec-7] (HibernateTransactionManager.java:652) - Committing Hibernate transaction on Session [org.hibernate.impl.SessionImpl@573a46b6]
DEBUG [http-bio-8080-exec-7] (JDBCTransaction.java:130) - commit
DEBUG [http-bio-8080-exec-7] (JDBCTransaction.java:223) - re-enabling autocommit
DEBUG [http-bio-8080-exec-7] (JDBCTransaction.java:143) - committed JDBC Connection
DEBUG [http-bio-8080-exec-7] (ConnectionManager.java:325) - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
DEBUG [http-bio-8080-exec-7] (HibernateTransactionManager.java:734) - Closing Hibernate Session [org.hibernate.impl.SessionImpl@573a46b6] after transaction
DEBUG [http-bio-8080-exec-7] (SessionFactoryUtils.java:789) - Closing Hibernate Session
DEBUG [http-bio-8080-exec-7] (ConnectionManager.java:464) - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG [http-bio-8080-exec-7] (ConnectionManager.java:325) - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
this is how i get my session
public Session getSession(){
return (this.factory.getCurrentSession()==null)?
this.factory.getCurrentSession() : this.factory.openSession();
}
Upvotes: 0
Views: 836
Reputation: 692121
The problem is likely caused by the way you get a session. The session you get is in no way linked to the Spring transaction manager, and what you're doing with it is done outside of the Spring transaction. You should define a Spring-based session factory in your spring context, and inject it in your DAO, as explained in the documentation.
Upvotes: 1