我使用Spring MVC 3.2.6,Hibernate 4,Oracle和weblogic 10.3.6
我想在Oracle数据库中使用Hibernate和JPA提交一个实体。在服务器控制台中,我没有看到错误。但是当我检查Oracle表时,不会记录新记录。
这是我的JPA实体
package eusurvey.modelA.daos;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the PREFERENCIAS database table.
*
*/
@Entity
@Table(name="PREFERENCIAS")
public class Preferencia implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="PREFERENCIAS_ID")
private long preferenciasId;
private String nombre;
private String valor;
public Preferencia() {
}
public long getPreferenciasId() {
return this.preferenciasId;
}
public void setPreferenciasId(long preferenciasId) {
this.preferenciasId = preferenciasId;
}
public String getNombre() {
return this.nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getValor() {
return this.valor;
}
public void setValor(String valor) {
this.valor = valor;
}
}
服务类AdministrationService.java.I使用createPreferencia方法。
package eusurvey.services;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.log4j.Logger;
import eusurvey.modelA.daos.EncuestaSem;
import eusurvey.modelA.daos.Encuestado;
import eusurvey.modelA.daos.Preferencia;
import eusurvey.tools.ConversionTools;
@Service("administrationService")
public class AdministrationService {
private static final Logger logger = Logger.getLogger(AdministrationService.class);
@Resource(name="sessionFactory")
private SessionFactory sessionFactory;
@Transactional
public void createEncuestado(Encuestado encuestado) throws Exception {
Session session = sessionFactory.getCurrentSession();
session.save(encuestado);
}
@Transactional(readOnly = false)
public void createEncuesta(EncuestaSem encuesta) throws Exception {
Session session = sessionFactory.getCurrentSession();
logger.info("fecha inicio "+encuesta.getFechaInicio());
session.save(encuesta);
logger.info("Grabada encuesta");
}
@Transactional(readOnly = false)
public void createPreferencia(Preferencia preferencia) throws Exception {
Session session = sessionFactory.getCurrentSession();
session.save(preferencia);
logger.info("Grabada prefencia");
}
}
我调用createPreferencia方法的Java是
administrationService.createPreferencia(preferencia);
控制台服务器消息
04:46 DEBUG SQL:104 - select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
2016-07-05 14:04:46 DEBUG SequenceGenerator:128 - Sequence identifier generated: BasicHolder[java.lang.Long[1404]]
2016-07-05 14:04:46 DEBUG AbstractSaveEventListener:131 - Generated identifier: 1404, using strategy: org.hibernate.id.SequenceGenerator
2016-07-05 14:04:46 INFO AdministrationService:60 - Grabada prefencia
2016-07-05 14:04:46 INFO ConsultasArielService:213 - despues de commit preferencia
2016-07-05 14:04:46 DEBUG AbstractTransactionImpl:173 - committing
2016-07-05 14:04:46 DEBUG JdbcTransaction:113 - committed JDBC Connection
2016-07-05 14:04:46 DEBUG JdbcTransaction:126 - re-enabling autocommit
2016-07-05 14:04:46 DEBUG LogicalConnectionImpl:314 - Releasing JDBC connection
2016-07-05 14:04:46 DEBUG LogicalConnectionImpl:332 - Released JDBC connection
2016-07-05 14:04:46 DEBUG ConnectionProxyHandler:219 - HHH000163: Logical connection releasing its physical connection
2016-07-05 14:04:46 DEBUG ConnectionProxyHandler:219 - HHH000163: Logical connection releasing its physical connection
2016-07-05 14:04:46 DEBUG ConnectionProxyHandler:219 - HHH000163: Logical connection releasing its physical connection
2016-07-05 14:04:46 DEBUG ConnectionProxyHandler:219 - HHH000163: Logical connection releasing its physical connection
2016-07-05 14:04:46 INFO EncuestaController:92 - EncuestaControler despues de grabas encuesta
如何检查SQL句子?
答案 0 :(得分:0)
如果您使用<tx:annotation-driven/>
您必须像这样定义transactionManager:
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
id必须是txManager,否则你应该像这样定义transactionManager的特殊id。
<tx:annotation-driven transaction-manager="myTxManager" />
你应该改变你的代码@Transactional(propagation = Propagation.REQUIRED,readOnly = false)
。
如果您没有定义Propagation
,则默认值为SUPPORTS
,如果外部没有事务,则代码将在事务中执行。所以没有事务开始,也没有事务提交。
总而言之,只需使用@Transactional(propagation = Propagation.REQUIRED,readOnly = false)