I facing issue when i am getting id after saving the claimDetail object then to get the id of that saved object it is coming 0 . Actually I want get that saved object Id .But it not coming. I did not work with JPA. I have created a Spring Boot application for scheduling .
Here is my ClaimDetails.java entity class:
#Entity
#Table(name = "claimtrans")
public class ClaimTrans {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
claimDetail.setActive(1);
claimDetail.setVersion(new Long(1));
claimDetail.setCreatedBy(new Long(1));
claimDetail.setCreatedDate(new Date());
claimDetailService.saveClaimDetail(claimDetail);
int temp =claimDetail.getID()
temp is 0;
Here is my JpaRepositoryFactory.java:
#Service
public class ClaimDetailService {
private JpaRepositoryFactory jpaRepositoryFactory;
#Autowired
public ClaimDetailService(JpaRepositoryFactory jpaRepositoryFactory) {
this.jpaRepositoryFactory = jpaRepositoryFactory;
}
#Transactional
public void saveClaimDetail(ClaimDetail claimDetail) {
JpaRepository<ClaimDetail, Long> mailAuditLogLongJpaRepository = jpaRepositoryFactory.getRepository(ClaimDetail.class);
mailAuditLogLongJpaRepository.save(claimDetail);
}
public List<ClaimDetail> getAllClaimDetail() {
JpaRepository<ClaimDetail, Long> mailAuditLogLongJpaRepository = jpaRepositoryFactory.getRepository(ClaimDetail.class);
return mailAuditLogLongJpaRepository.findAll();
}
}
Here is my JPA Factory.
#Component
public class JpaRepositoryFactory {
#PersistenceContext
private EntityManager entityManager;
public <T> T getRepository(Class clazz) {
notNull(clazz);
notNull(entityManager);
T crudRepository = (T) new SimpleJpaRepository(clazz, entityManager);
return crudRepository;
}
}
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>org.sam.application.Application</start-class>
<java.version>1.6</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
Anyone can help me please how to fix this issue ?
Thanks
Sitansu
The JPA spec doesn't guarantee that the provided entity object will be updated after saving it. To get the saved JPA entity you have to use the return value of the save() method. For example, your service could be changed this way:
#Service
public class ClaimDetailService {
...
#Transactional
public ClaimDetail saveClaimDetail(ClaimDetail claimDetail) {
JpaRepository<ClaimDetail, Long> mailAuditLogLongJpaRepository = jpaRepositoryFactory.getRepository(ClaimDetail.class);
return mailAuditLogLongJpaRepository.save(claimDetail);
}
...
}
And your sample code would be:
claimDetail.setActive(1);
claimDetail.setVersion(new Long(1));
claimDetail.setCreatedBy(new Long(1));
claimDetail.setCreatedDate(new Date());
ClaimDetail savedClaimDetail = claimDetailService.saveClaimDetail(claimDetail);
int temp = savedClaimDetail.getID()
Also, although not directly related to your problem, you don't need to create the Spring Data repositories the way you have done it. Just create your own interface extending JPARepository.
Write a configuration class and do something like this. Use JpaRepository
#Configuration
public class ClaimDetailService {
public interface ClaimDetailRepository extends JpaRepository<Claimtrans, String>{
ClaimDetail findById(String id);
}
#Autowired
ClaimDetailRepository claimDetailRepository;
#Autowired
public void save(){
ClaimTrans claimDetail=new ClaimTrans();
claimDetail.setId(UUID.randomUUID.toString());
claimDetail.setActive(1);
claimDetail.setVersion(new Long(1));
claimDetail.setCreatedBy(new Long(1));
claimDetail.setCreatedDate(new Date());
claimDetailRepository.save(claimDetail);
int temp =claimDetailRepository.findById(claimDetail.getId());
}
}
Related
I have a problem with Spring Boot application. I want to connect a MongoDB database and a MySql database in my Spring boot application. I would to know if it is possible, in positive case How I can make this multiple connection. I had made a try based on an example with Mysql and Post without success. So I'm wondering if someone have an easy example to know the method.
thanks
It is possible to do this.you will have create different configuration for different datasources. This link has good examples on that
http://www.baeldung.com/spring-data-jpa-multiple-databases
Another useful stackoverflow question: Spring Boot Configure and Use Two DataSources
To get started with mongo and mysql , you can follow example from spring.io guides.
https://spring.io/guides/gs/accessing-data-mongodb/
https://spring.io/guides/gs/accessing-data-mysql/
EDIT :
I have created this one example, merging two samples above
package hello;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import hello.model.Customer;
import hello.model.User;
import hello.mongodao.CustomerRepository;
import hello.mysqldao.UserRepository;
#EnableMongoRepositories(basePackageClasses = CustomerRepository.class)
#EnableJpaRepositories (basePackageClasses = UserRepository.class)
#SpringBootApplication
public class Application implements CommandLineRunner {
#Autowired
private CustomerRepository repository;
#Autowired
private UserRepository userRepository;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
public void run(String... args) throws Exception {
System.out.println("getting data from Mongo");
repository.deleteAll();
// save a couple of customers
repository.save(new Customer("Alice", "Smith"));
repository.save(new Customer("Bob", "Smith"));
// fetch all customers
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();
// fetch an individual customer
System.out.println("Customer found with findByFirstName('Alice'):");
System.out.println("--------------------------------");
System.out.println(repository.findByFirstName("Alice"));
System.out.println("Customers found with findByLastName('Smith'):");
System.out.println("--------------------------------");
for (Customer customer : repository.findByLastName("Smith")) {
System.out.println(customer);
}
System.out.println("gettting data from mysql");
userRepository.deleteAll();
// save a couple of customers
userRepository.save(new User("Alice", "Alice#Smith.com"));
userRepository.save(new User("Bob", "Bob#Smith.com"));
// fetch all customers
System.out.println("Users found with findAll():");
System.out.println("-------------------------------");
for (User user : userRepository.findAll()) {
System.out.println(user);
}
}
}
CustomerRepository.java
package hello.mongodao;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import hello.model.Customer;
public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
UserRepository.java
package hello.mysqldao;
import org.springframework.data.repository.CrudRepository;
import hello.model.User;
// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete
public interface UserRepository extends CrudRepository<User, Long> {
}
Customer.java
package hello.model;
import org.springframework.data.annotation.Id;
public class Customer {
#Id
public String id;
public String firstName;
public String lastName;
public Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}
User.java
package hello.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity // This tells Hibernate to make a table out of this class
public class User {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String email;
public User() {
// TODO Auto-generated constructor stub
}
public User(String string, String string2) {
// TODO Auto-generated constructor stub
name = string;
email = string2;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Override
public String toString() {
return String.format(
"User[id=%s, name='%s', email='%s']",
id, name, email);
}
}
application.properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.data.mongodb.uri=mongodb://localhost:27017/local
You really don't need to make additional config and property files because MongoDB has different property names than sql so all you will need is an application.properties file
application.properties
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/dbName?useUnicode=yes&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=
spring.data.mongodb.uri=mongodb://localhost:27017
spring.data.mongodb.database=dbName
example models
MongoDB document
import org.springframework.data.mongodb.core.mapping.Document;
import javax.persistence.Id;
#Document("Gyros")
public class Gyros {
public Gyros(String description) {
this.description = description;
}
#Id
public String id;
public String description;
}
Mysql JPA entity
import javax.persistence.*;
#Entity
#Table(name = "Kebab")
public class Kebab {
public Kebab(String description) {
this.description = description;
}
public Kebab() {
}
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public int id;
public String description;
}
MongoDB repository
#Repository
public interface GyrosRepository extends MongoRepository<Gyros, String> {
}
Mysql Jpa repository
#Repository
public interface KebabRepository extends JpaRepository<Kebab, Integer> {
}
TestService
#org.springframework.stereotype.Service
public class Service {
private final GyrosRepository gyrosRepository;
private final KebabRepository kebabRepository;
#Autowired
public Service(GyrosRepository gyrosRepository, KebabRepository kebabRepository) {
this.gyrosRepository = gyrosRepository;
this.kebabRepository = kebabRepository;
}
#PostConstruct
void test() {
this.gyrosRepository.insert(new Gyros("ham ham"));
this.kebabRepository.saveAndFlush(new Kebab("yum yum"));
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>stack</artifactId>
<version>1.0-SNAPSHOT</version>
<name>stackoverflow</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
I also faced the same kind of problem once. I had to connect my spring boot application to two different databases. One was Mongo db and other was Postgres db.
You can see that i have used both JPA as well as spring-boot-starter-data-mongodb. Still my project is running absolutely fine.Hope for you also it work successfully. There are suggestions over the internet to not use JPA but i am not able to use JPA repository without include JPA.
Here I am posting the solution which worked for me.
Hope it helps someone:
application.properties file:
MONGODB (MongoProperties)
spring.data.mongodb.uri=mongodb://XX.XX.XX.XX:27017/testdb
#POSTGRES properties
spring.datasource.platform=postgres
spring.datasource.url= jdbc:postgresql://localhost:5432/database_name
spring.datasource.username=postgres_usr_name
spring.datasource.password=postgres_pwd
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
My pom dependencies:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
To access my data Using Repositories:
(i): MONGO REPOSITORY
import org.springframework.data.mongodb.repository.MongoRepository;
public interface MRepositories extends MongoRepository<YourEntityClass, String>{
}
(ii): JPA repository
#Repository public interface PostGresRepo extends JpaRepository<TestEntity,Long> {}
The Problem i have resulted out of this tutorial.
My Problem is that i always run in the issue that my user is read out of the database but i don't get authenticated. On the view it always shows the error message "Invalid Username or Password". My Console-output shows no errors. When i debugged through my authentication process there were no unclear behaviours where the error could come from.
In my pom.xml i used the following dependencies.
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
My CustomUserDetailsService.java looks like this.
#Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService {
private final UserRepository userRepository;
private final UserRolesRepository userRolesRepository;
#Autowired
public CustomUserDetailsService(UserRepository userRepository,
UserRolesRepository userRolesRepository) {
this.userRepository = userRepository;
this.userRolesRepository = userRolesRepository;
}
#Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUserName(username);
if (user == null) {
throw new UsernameNotFoundException("No user present with username " + username);
}
else {
List<String> userRoles = userRolesRepository.findRoleByUserName(username);
return new CustomUserDetails(user, userRoles);
}
}
}
CustomUserDetails.java
public class CustomUserDetails extends User implements UserDetails {
private static final long serialVersionUID = 1L;
private List<String> userRoles;
public CustomUserDetails(User user, List<String> userRoles) {
super(user);
this.userRoles = userRoles;
}
#Override
public Collection< ? extends GrantedAuthority> getAuthorities() {
String roles = StringUtils.collectionToCommaDelimitedString(userRoles);
return AuthorityUtils.commaSeparatedStringToAuthorityList(roles);
}
#Override
public boolean isAccountNonExpired() {
return false;
}
#Override
public boolean isAccountNonLocked() {
return false;
}
#Override
public boolean isCredentialsNonExpired() {
return false;
}
}
WebSecurityConfig.java
#Configuration
#EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
#Autowired
private UserDetailsService userDetailsService;
#Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordencoder());
}
#Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/home", "/images/**", "/css/**", "/js/**", "/loadEvents")
.permitAll().anyRequest().authenticated().and().formLogin().loginPage("/login")
.permitAll().and().logout().permitAll();
}
#Bean(name = "passwordEncoder")
public PasswordEncoder passwordencoder() {
return new BCryptPasswordEncoder();
}
}
As i mentioned before i don't think that the error occures on the database. I used the database scheme out of the tutorial. I also used User.java UserRole.java and the two repositories out of the tutorial.
My Application.java looks like this.
#SpringBootApplication
#EnableJpaRepositories(basePackages = "<package>")
#EntityScan(basePackages = "<package>")
public class Application {
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}
Update 1: Link to Git Project
https://github.com/pStuetz/Speeddating4SO/tree/master
You maybe have to Edit the src/main/resources/application.properties to support your database. I included the sql script which i used to create my database tables.
I've enabled trace logging in application.properties by adding this row
logging.level.=TRACE
and saw the error message
org.springframework.security.authentication.LockedException: User account is locked
in the console.
Your custom user details class de.dhbw.stuttgart.speeddating.userhandling.service.impl.CustomUserDetails returns "false" from methods "isAccountNonExpired", "isAccountNonLocked" and "isCredentialsNonExpired". I guess the methods should return the value of the property "enabled" from the class de.dhbw.stuttgart.speeddating.userhandling.service.User.
After changing all those "false" to "true", the login procedure started to work as expected.
I am trying to add #NotNull constraint into my Person object but I still can #POST a new Person with a null email. I am using Spring boot rest with MongoDB.
Entity class:
import javax.validation.constraints.NotNull;
public class Person {
#Id
private String id;
private String username;
private String password;
#NotNull // <-- Not working
private String email;
// getters & setters
}
Repository class:
#RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends MongoRepository<Person, String> {
}
Application class:
#SpringBootApplication
public class TalentPoolApplication {
public static void main(String[] args) {
SpringApplication.run(TalentPoolApplication.class, args);
}
}
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
...
When I #POST a new object via Postman like:
{
"username": "deadpool",
"email": null
}
I still get STATUS 201 created with this payload:
{
"username": "deadpool",
"password": null,
"email": null
....
....
}
I had the same problem, but just enabling validation didn't work for me, this did work with both JPA and MongoDb to save anyone else spending ages on this. Not only does this get validation working but I get a nice restful 400 error rather than the default 500.
Had to add this to my build.gradle dependencies
compile('org.hibernate:hibernate-validator:4.2.0.Final')
and this config class
#Configuration
public class CustomRepositoryRestConfigurerAdapter extends RepositoryRestConfigurerAdapter {
#Bean
public Validator validator() {
return new LocalValidatorFactoryBean();
}
#Override
public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
validatingListener.addValidator("afterCreate", validator());
validatingListener.addValidator("beforeCreate", validator());
validatingListener.addValidator("afterSave", validator());
validatingListener.addValidator("beforeSave", validator());
}
}
i found it better to make my own version of #NotNull annotation which validates empty string as well.
#Documented
#Constraint(validatedBy = NotEmptyValidator.class)
#Target({ElementType.METHOD, ElementType.FIELD})
#Retention(RetentionPolicy.RUNTIME)
public #interface NotEmpty {
String message() default "{validator.notEmpty}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
public class NotEmptyValidator implements ConstraintValidator<NotEmpty, Object> {
#Override
public void initialize(NotEmpty notEmpty) { }
#Override
public boolean isValid(Object obj, ConstraintValidatorContext cxt) {
return obj != null && !obj.toString().trim().equals("");
}
}
You can either use the following code for validating
#Configuration
#Import(value = MongoAutoConfiguration.class)
public class DatabaseConfiguration extends AbstractMongoConfiguration
{
#Resource
private Mongo mongo;
#Resource
private MongoProperties mongoProperties;
#Bean
public ValidatingMongoEventListener validatingMongoEventListener() {
return new ValidatingMongoEventListener(validator());
}
#Bean
public LocalValidatorFactoryBean validator() {
return new LocalValidatorFactoryBean();
}
#Override
protected String getDatabaseName() {
return mongoProperties.getDatabase();
}
#Override
public Mongo mongo() throws Exception {
return mongo;
}
}
Normally, the #RestRepository will resolve into a controller than handles validation by itself, except if you Override the default behavior or it by including some #HandleBeforeSave, #HandleBeforeCreate, ... into your code.
A solution is to remove the #HandleBeforeSave, #HandleBeforeCreate, ...
and then spring will handle the validation again.
Or if you want to keep them, you can provide a handler for any object validation like this:
#Component
#RepositoryEventHandler
public class EntityRepositoryEventHandler {
#Autowired
private Validator validator;
#HandleBeforeSave
#HandleBeforeCreate
public void validate(Object o) {
Set<ConstraintViolation<Object>> violations = this.validator.validate(o);
if (!violations.isEmpty()) {
ConstraintViolation<Object> violation = violations.iterator().next();
// do whatever your want here as you got a constraint violation !
throw new RuntimeException();
}
}
}
I would like to know if anyone else has used hibernate-envers with mysql in a Spring-boot application using the interface CrudRepository and has had trouble deleting entities with collections. I put together an example application with a test that demonstrates the exception that is generated.
The example can be cloned from https://github.com/LindesRoets/test-delete.git
You will need mysql running with a database called test_delete
CREATE DATABASE IF NOT EXISTS `test_delete` DEFAULT CHARACTER SET utf8;
Run the test
mvn test
You should see the following exception:
2015-08-11 21:36:28.725 ERROR 3855 --- [ main] org.hibernate.AssertionFailure : HHH000099: an assertion failure occured (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session): java.lang.NullPointerException
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.926 sec <<< FAILURE! - in com.dcp.test.AuthorRepositoryTest
testAuthorCRUD(com.dcp.test.AuthorRepositoryTest) Time elapsed: 0.163 sec <<< ERROR!
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.engine.internal.StatefulPersistenceContext.getLoadedCollectionOwnerOrNull(StatefulPersistenceContext.java:756)
at org.hibernate.event.spi.AbstractCollectionEvent.getLoadedOwnerOrNull(AbstractCollectionEvent.java:75)
at org.hibernate.event.spi.InitializeCollectionEvent.<init>(InitializeCollectionEvent.java:36)
at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:1931)
at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:558)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:260)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:294)
at java.util.AbstractCollection.addAll(AbstractCollection.java:343)
at org.hibernate.envers.internal.entities.mapper.relation.AbstractCollectionMapper.mapCollectionChanges(AbstractCollectionMapper.java:162)
at org.hibernate.envers.internal.entities.mapper.relation.AbstractCollectionMapper.mapModifiedFlagsToMapFromEntity(AbstractCollectionMapper.java:212)
at org.hibernate.envers.internal.entities.mapper.MultiPropertyMapper.map(MultiPropertyMapper.java:105)
at org.hibernate.envers.internal.synchronization.work.DelWorkUnit.generateData(DelWorkUnit.java:66)
at org.hibernate.envers.internal.synchronization.work.AbstractAuditWorkUnit.perform(AbstractAuditWorkUnit.java:76)
at org.hibernate.envers.internal.synchronization.AuditProcess.executeInSession(AuditProcess.java:119)
Below is all the source code as it appears in the sample application.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-delete</name>
<description></description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.test.Application</start-class>
<java.version>1.8</java.version>
<spring-boot-version>1.2.5.RELEASE</spring-boot-version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/*Test*.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.3</version>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>${spring-boot-version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
</dependency>
</dependencies>
</project>
application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/test_delete
spring.datasource.username =
spring.datasource.password =
# Specify the DBMS
spring.jpa.database = MYSQL
spring.datasource.driverClassName = com.mysql.jdbc.Driver
# Show or not log for each sql query
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
#envers
spring.jpa.properties.org.hibernate.envers.global_with_modified_flag=true
spring.jpa.properties.org.hibernate.envers.store_data_at_delete=true
Application.java
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
BaseEntity.java
#MappedSuperclass
#Audited
public abstract class BaseEntity implements Serializable {
#GeneratedValue(generator = "guid")
#GenericGenerator(name = "guid", strategy = "guid")
#Column(columnDefinition = "CHAR(36)")
#Id
protected String id;
#Temporal(TemporalType.TIMESTAMP)
protected Date dateCreated = new Date();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Date getDateCreated() {
return dateCreated;
}
public void setDateCreated(Date dateCreated) {
this.dateCreated = dateCreated;
}
private static final long serialVersionUID = -8371628134270930829L;
}
Author.java
#Entity
#Audited
public class Author extends BaseEntity{
private String email;
private String name;
#OneToMany(mappedBy = "author")
private List<Book> books = new ArrayList<>();
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}
Book.java
#Entity
#Audited
public class Book extends BaseEntity {
private String title;
private String genre;
#ManyToOne
private Author author;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
}
AuthorRepository.java
#Transactional
public interface AuthorRepository extends CrudRepository<Author, String> {
}
AuthorRepositoryTest.java
#RunWith(SpringJUnit4ClassRunner.class)
#SpringApplicationConfiguration(classes = Application.class)
public class AuthorRepositoryTest {
#Autowired
private AuthorRepository authorRepo;
#Test
public void testAuthorCRUD() {
Author author = new Author();
author.setName("Test Name");
author.setEmail("test#mail.com");
Author savedAuthor = authorRepo.save(author);
Assert.assertNotNull(savedAuthor);
authorRepo.delete(savedAuthor);
}
}
If you comment out the #Audited from the entity classes Book, Author and BaseEntity the test pass just fine.
Does anyone know how to make the delete functionality work with envers?
There are two ways that I found through trial and error to make the delete operation work as specified in the problem.
You can either specify the collection to load eagerly
#OneToMany(mappedBy = "author", fetch = FetchType.EAGER)
Or you can cascade the delete operation
#OneToMany(mappedBy = "author", cascade = CascadeType.REMOVE)
This is the minimum configuration that you will need to make the test pass as specified in the problem.
Had the same issue, fixed by doing an explicit join to the relationship in question, also I think it's a common thing to delete child relationships first as well so it might be a thing you will have to do anyways.
I think in the context of envers you have to use repository delete methods to be able to save the revisions on delete.
I have a RESTful service with some methods. Methods are annotated by #POST and #Consumes(MediaType.APPLICATION_JSON).
I have wrapper for request parameters:
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class RequestWrapper {
#XmlElement
private SomeInfo someInfo = new SomeInfo();
#XmlElement
#XmlElementWrapper
private List<RequestParameter> requestParameters = new ArrayList<>();
}
public class SomeInfo {
public String field1;
public String field2;
}
public class RequestParameter {
public String key;
public String value;
}
I make request to my service. Body of the POST message:
{"someInfo":{"field1":"b","field2":"c"},"requestParameters":[{"key":"1","value":"2"},{"key":"3","value":"4"}]}
I see that someInfo values processed and accesible via RequestWrapper. But List<RequestParameter> requestParameters has zero length.
What should I do to messages been processed normally?
P.S.
I use Glassfish 4.0. Dependencies of the maven module:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
omg, I have found magic bullet
Delete #XmlAccessorType(XmlAccessType.FIELD)
Delete #XmlElementWrapper
make public getters/setters for each field
#XmlRootElement
public class RequestWrapper {
#XmlElement
private SomeInfo someInfo = new SomeInfo();
#XmlElement
private List<RequestParameter> requestParameters = new ArrayList<>();
// getters/setters for each field
}