0
Javaアプリケーションを作成しようとしていますが、@ Transactionalアノテーションを作成できません。@TransactionalがSessionFactoryと連携していません
Iは、例えばPaymentDaoクラスている:最初のオブジェクトは、常にデータベースに保存され、
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
public class PaymentDao {
private SessionFactory sessionFactory;
@Autowired
public PaymentDao(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Transactional
public void add(Payment payment) {
sessionFactory.getCurrentSession()
.save(payment);
if (payment != null) throw new RuntimeException();
sessionFactory.getCurrentSession()
.save(payment);
}
}
とのRuntimeExceptionもかかわらず、スローされます。
public void add(Payment payment) {
try {
sessionFactory.getCurrentSession().getTransaction().begin();
sessionFactory.getCurrentSession()
.save(payment);
if (payment != null) throw new RuntimeException();
sessionFactory.getCurrentSession()
.save(payment);
sessionFactory.getCurrentSession().getTransaction().commit();
} catch(RuntimeException e) {
sessionFactory.getCurrentSession().getTransaction().rollback();
}
}
それが動作し、何の実体が保存されません:
は私がしようとすると、そのようにやっていることに気づきました。
@Transactional注釈は同じにしてはいけませんか?私は何を逃したのですか? はここに私の設定クラスです:
@Configuration
@EnableTransactionManagement
public class Config {
@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory
hemf){
return hemf.getSessionFactory();
}
}
編集:私はサービスからそれを呼んでいる :
コントローラにautowiredさ@Service
public class PaymentService {
@Autowired
private PaymentDao paymentDao;
public void add(PaymentDto paymentDto) throws IOException {
Payment payment = paymentDto.toEntity();
paymentDao.add(payment);
}
}
。
休止状態の設定は簡単です:
spring.datasource.url= jdbc:postgresql://localhost:5432/testT
spring.datasource.username=postgres
spring.datasource.password=admin1
spring.jpa.properties.hibernate.hbm2ddl.import_files_sql_extractor
=org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor
spring.jpa.properties.hibernate.current_session_context_class
=org.springframework.orm.hibernate4.SpringSessionContext
spring.jpa.hibernate.ddl-auto=create-drop
logging.level.org.springframework.transaction.interceptor=TRACE
あなたが書いているバラエティのDAOロジックは、Spring Dataによって自動生成されます。 – chrylis
ええ、私は知っていますが、私はちょうど今学んでいて、他の方法も知ることを試みています。 – Besanouno
@Besanounoどこからaddメソッドを呼び出していますか? –