I'm trying to save entity in repository but it does not work at all. Repository is Autowired and in runtime I use saveAndFlush to save entity. I'm using PostgreSQL. Above test methods I added comments with explanation what is going on. I expected that method saveAndFlush should work but it did not. I can not find why.
#Transactional
public class TestClass{
#Autowired private MyRepository repository;
#Autowired private EntityManager entityManager;
// Working version
public void writingToRepositoryWorking() {
entityManager.getTransaction().begin();
entityManager.persist(new MyData(99));
entityManager.getTransaction().commit();
}
// not working and throws exception :
// TransactionRequiredException: no transaction is in progress
public void writingToRepositoryNotWorking() {
repository.saveAndFlush(new MyData(99));
}
// not working, no exception, no data in repository,
// but auto generated ID is incremented
public void writingToRepositoryNotWorkingToo() {
repository.save(new MyData(99));
}
}
repository interface file
#Repository
#Transactional
public interface MyRepository extends JpaRepository<MyData, Long> {}
MyData file
#Entity(name = "myData")
public class MyData {
#Id #GeneratedValue(strategy = GenerationType.AUTO) long id;
private int testValue;
public MyData() { }
public BugData(int testValue) {
this.testValue = testValue;
}
public long getId() {
return id;
}
public int getTestValue() {
return testValue;
}
}
ApplicationConfiguration file
#Configuration
#EnableJpaRepositories("com.mypackage.app")
#EnableTransactionManagement
#PropertySource("classpath:application.properties")
#EnableWebMvc
class ApplicationConfiguration extends WebMvcConfigurationSupport {
#Value("${jdbc.url}") private String KEY_JDBC_URL;
#Value("${jdbc.username}") private String KEY_JDBC_USERNAME;
#Value("${jdbc.password}") private String KEY_JDBC_PASSWORD;
#Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
#Bean
#Autowired
public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setPackagesToScan("com.mypackage.app");
factory.setHibernateProperties(hibernateProperties());
return factory;
}
public Properties hibernateProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.hbm2ddl.auto", "update");
return properties;
}
#Bean
#Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
return new HibernateTransactionManager(sessionFactory);
}
#Bean
public DataSource dataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl(KEY_JDBC_URL);
dataSource.setUsername(KEY_JDBC_USERNAME);
dataSource.setPassword(KEY_JDBC_PASSWORD);
return dataSource;
}
#Bean
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource());
em.setPackagesToScan("com.mypackage.app");
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
em.setJpaProperties(hibernateProperties());
em.afterPropertiesSet();
return em.getObject();
}
#Bean
public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}
...
}
For starter, you're actually working on 2 different EntityManager in your non-working test case:
EntityManager autowired into your test by Spring (this one is singleton and should be avoided anyway) ,other is
EntityManager created by the EntityManagerFactory configured in your ApplicationConfiguration.
At the same time, you also have another Session running along side the aforementioned 2 EntityManagers due to your configuration of Hibernate SessionFactory. Additionally, because of the configured HibernateTransactionManager, all transactions created by #Transactional are bound to the Hibernate's Session created by SessionFactory and the EntityManager used by your Repository certainly has no way to know about it. This is why TransactionRequiredException was thrown when your Repository tried to persist data.
To fix it, you may consider removing the Hibernate's SessionFactory and switch the transaction manager to a JpaTransactionManager. Then, #Transactional on your Repository will have the effect of creating a new transaction and binding it to the existing EntityManager that is known to Spring.
One side note is that the #Transactional on your TestClass doesn't help at all as the instance of this class is not instantiated and managed by Spring. To make this work, a proper configuration of transactional test class needs to be provided as described here: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html.
Hope this helps.
Related
I'm getting FooBar is not mapped exception, even though I have placed all the necessary annotations:
The Entity annotation in the model class:
model/FooBar.java*
#Data
#Entity
#Table(name = "ORDERS")
public class FooBar {}
The Repository, PersistenceContext and Transactioinal in DAO. Also, I referenced it by the name of the class, "FooBar", correctly
db/FooBarDao.java
#Repository
public class OrderDAO {
#PersistenceContext
private EntityManager em;
#Transactional
public List<FooBar> getFooBars() {
return em.createQuery(
"select fb from FooBar fb",
FooBar.class
).getResultList();
}
}
Here is the configuration to get it all working:
conf/DbConfig.java
#EnableTransactionManagement
#Configuration
#ComponentScan(basePackages = {"project.db"})
public class DbConfig {
#Autowired
public Environment env;
#Bean
public DataSource dataSource() {
System.out.println("Configuring database!!!!!!!!");
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("org.hsqldb.jdbcDriver");
ds.setUrl("jdbc:hsqldb:mem:myjdbc");
new JdbcTemplate(ds)
.update(FileUtil.readFile2("./src/main/java/project/db/sql/initialize.sql"));
return ds;
}
#Bean
public PlatformTransactionManager transactionManager(
EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
#Bean
public EntityManagerFactory entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setPersistenceProviderClass(HibernatePersistenceProvider.class);
factory.setPackagesToScan("model");
factory.setDataSource(dataSource());
factory.setJpaProperties(additionalProperties());
factory.afterPropertiesSet();
return factory.getObject();
}
private Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", "validate");
properties.setProperty("hibernate.dialect",
"org.hibernate.dialect.HSQLDialect");
properties.setProperty("hibernate.show_sql", "true");
properties.setProperty("hibernate.format_sql", "true");
return properties;
}
}
It's not finding the mapping, even though I have set the annotations correctly, and referenced the class correctly. So the problem is most likely with scanning for the model class.
My project structure is /src/main/java/project/...
So, scanning for model:
factory.setPackagesToScan("model");
Will not find any beans. Instead, I need to do
factory.setPackagesToScan("project.model");
Which fixes the problem.
I am creating simple spring boot application. I used spring transaction management to handle the transaction. Here is my code.
ServiceImpl class
#Service("orderService")
public class OrderServiceImpl implements OrderService {
#Autowired
private CustomerDao customerDao;
#Autowired
private OrderDao orderDao;
#Transactional(rollbackFor = Exception.class)
#Override
public Long placeOrder(OrderPlacementRequest request) {
customerDao.save(request.getCustomer());
return orderDao.placeOrder(request.getOrder());
}
}
OrderDaoImpl class,
#Repository("orderDao")
public class OrderDaoImpl extends AbstractHibernateDao implements OrderDao {
#Override
public Long placeOrder(Order order) {
throw new RuntimeException("Test Error Message");
}
}
Configuration class,
#Configuration
#EnableTransactionManagement
public class HibernateConfig {
//Other Configurations
#Autowired
private final Environment environment;
#Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(environment.getRequiredProperty("spring.datasource.url"));
dataSource.setUsername(environment.getRequiredProperty("spring.datasource.username"));
dataSource.setPassword(environment.getRequiredProperty("spring.datasource.password"));
return dataSource;
}
#Bean
public DataSourceTransactionManager transactionManager() {
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource());
return transactionManager;
}
}
application.properties
spring.aop.proxy-target-class=true
OrderController class
#RestController
#RequestMapping("/order")
public class OrderController {
#Autowired
private OrderService orderService;
#RequestMapping(value = "/place", method = RequestMethod.POST)
public Long placeOrder(#RequestBody OrderPlacementRequest request) {
return orderService.placeOrder(request);
}
}
In my case even though second method placeOrder failed. Customer will be saved in mysql database. What I wanted is to rollback the customer saving method. I went through few articles on transaction management including spring docs and stackoverflow. Still can not find the problem.
Updated with Controller class.
This question already has an answer here:
JpaRepository won't save data
(1 answer)
Closed 2 years ago.
I have a problem creating a Spring project using Spring Data JPA and Hibernate. When I called save method on the repository, it didn't insert my object to the database.
Hibernate config class:
#Configuration
#EnableTransactionManagement
#ComponentScan({ "com.app.config" })
#PropertySource(value = { "classpath:application.properties" })
#EnableJpaRepositories(basePackages = { "com.app.repository" })
public class HibernateConfiguration {
#Autowired
private Environment environment;
#Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.app.model", "com.app.repository" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
#Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(new String[] { "com.app.model", "com.app.repository" });
factory.setDataSource(dataSource());
factory.setJpaProperties(hibernateProperties());
factory.afterPropertiesSet();
return factory.getObject();
}
#Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
#Bean
#Autowired
public HibernateTemplate getHibernateTemplate(SessionFactory sessionFactory) {
HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
return hibernateTemplate;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
return properties;
}
#Bean
#Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
#Bean
#Autowired
public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
return entityManagerFactory.createEntityManager();
}
}
Repository class:
#Repository
public interface CityRepository extends BaseRepository<City, Integer> {
City findByName(String name);
List<City> findAll();
#Query("SELECT c.name FROM City c")
List<String> findAllCityName();
}
Service class:
#Service
public class CityService {
#Autowired
private CityRepository cityRepository;
public void saveCity(City city) {
return cityRepository.save(city);
}
}
BaseRepository is extended by CrudRepository in Spring JPA.
The code above runs without any errors but entity was not saved to the DB. Does anyone know what the issue with my code is?
I've resolved my problem by follow answer from JB Nizet above.
Drop the sessionFactory, drop the hibernateTemplate, replace the HibernateTransactionManager by a JpaTransactionManager. – JB Nizet
I have a pretty simple spring-data-jpa + Hibernate application, which stores customers data in MySql Database (the full source code is posted here).
The problem is that it runs insert and select queries very slowly, comparing to what I have through mysql CLI:
insert one row takes ~3600ms,
select all (just 2 rows) takes ~1700ms
both queries take in mysql CLI about 0.12s.
There is a similar problem discussed here, however in my case the measurements are way worse (even though I don't insert batch, it's just one simple row in DB). Is there any way to improve performance in Spring JPA/Hibernate?
Another question, is there any way to reduce size of spring-data-jpa and hibernate-entitymanager? I was able to exclude byte-buddy and jandex dependencies without harm to the program, but that's only couple Mbs (the shaded jar size is down from 19.6Mb to 16.6Mb)?
UPDATE
As per request here is the code (the all sources are here):
#Entity
#AttributeAccessor("field")
#Table(name = "customer")
public class Customer implements Serializable{
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name="id",unique=true, nullable=false, insertable=true, updatable=true)
#Type(type="long")
private Long id;
#Column(name = "name")
private String name;
//+ constructors, getters/setters
}
Here is Spring application and also saving (insert) customer:
public class Application {
private static ApplicationContext applicationContext;
static CustomerRepository customerRepository;
public static void main(String[] args) throws InterruptedException {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MySQLAutoconfiguration.class);
customerRepository = ctx.getBean(CustomerRepository.class);
runTest();
}
private static void runTest () throws {
...
//insert
Customer customerJohn = customerRepository.save(new Customer("John"));
//select
Customer foundEntity = customerRepository.findOne(customerJohn.getId());
...
}
And configuration:
#Configuration
#ComponentScan
#EnableJpaRepositories (basePackages = "com.vk.dal.repository")
#PropertySource("classpath:mysql.properties")
public class MySQLAutoconfiguration {
#Autowired
private Environment env;
#Bean
public DataSource dataSource() {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
dataSource.setUrl(env.getProperty("spring.datasource.url"));
dataSource.setUsername(env.getProperty("spring.datasource.username") != null ? env.getProperty("spring.datasource.username") : "");
dataSource.setPassword(env.getProperty("spring.datasource.password") != null ? env.getProperty("spring.datasource.password") : "");
return dataSource;
}
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.vk.dal.domain");
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
Properties properties = additionalProperties();
if (properties != null) {
em.setJpaProperties(properties);
}
return em;
}
#Bean
JpaTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
final Properties additionalProperties() {
final Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("mysql-hibernate.hbm2ddl.auto"));
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("mysql-hibernate.dialect"));
hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("mysql-hibernate.show_sql") != null ? env.getProperty("mysql-hibernate.show_sql") : "false");
return hibernateProperties;
}
}
Appreciate any help.
According to the book when i update a ElementCOllection List , i do not do the Transection.begin, the hibernate will auto commit, but i made a test on it, the result has something wrong
My Main.java is
public class Main {
private static UserService userService;
private static Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
ApplicationContext applicationContext =
new AnnotationConfigApplicationContext(RootConfig.class);
userService = applicationContext.getBean(UserService.class);
User user = new User("qwerty");
user.getMessages().add("hello,world");
userService.save(user);
User user1 = userService.findByName("qwerty");
user1.getMessages().add("ncjdksckds");
System.out.println(user);
}
}
and my configuration is here , coding according the book
#Configuration
#ComponentScan(basePackages = {"org.zhy"})
#EnableJpaRepositories(basePackages = {"org.zhy.repository"})
#EnableTransactionManagement
public class RootConfig {
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter ) {
LocalContainerEntityManagerFactoryBean emfb =
new LocalContainerEntityManagerFactoryBean();
emfb.setDataSource(dataSource);
emfb.setJpaVendorAdapter(jpaVendorAdapter);
emfb.setPersistenceUnitName("demo");
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
emfb.setJpaProperties(hibernateProperties);
emfb.setPackagesToScan("org.zhy.domain");
return emfb;
}
#Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
#Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
// some setting here such as url...
return ds;
}
#Bean
public JpaVendorAdapter jpaVendorAdapter() {
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setGenerateDdl(false);
adapter.setDatabase(Database.MYSQL);
adapter.setShowSql(true);
adapter.setDatabasePlatform("org.hibernate.dialect.MySQLDialect");
return adapter;
}
}
the Entity is here
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
#ElementCollection(fetch = FetchType.EAGER)
private List<String> messages = new ArrayList<String>();
//getter and setter
when i use user1.getMessages().add("ncjdksckds");
the database did not auto flush the new message into it , i want to know why????
Not sure which book you are referring to but the key about #ElementCollection in your case is that all operations are cascaded by default.
Assuming your service has all public methods marked as transactional, after you query for the user, it is a detached entity.. as it is from now on outside of any transactional scope.
In your code:
User user = new User("qwerty");
user.getMessages().add("hello,world");
userService.save(user); // new transaction start and finish
User user1 = userService.findByName("qwerty"); // new transaction start and finish
user1.getMessages().add("ncjdksckds"); // this change is outside of a transaction
in order to make that change persistend you would need to merge the user1 entity back into the persistence context:
userService.merge(user1);
Inside you would call:
entityManager.merge(user);