I have really strange behavior of ModelMapper framework combined with Spring and Hibernate 4. And after 2 days of searching through SO I still a little bit confused and can't figure out the reason of such strange behavior.
I have 3 classes: class A, class B and User class.
User class:
@Entity
@Table(name = "users")
@Data
@EqualsAndHashCode(of = "id")
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User implements UserDetails {
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
...
}
Class B:
@Entity
@Audited
@Table
@Data
@Builder
@EqualsAndHashCode(of = "id")
@AllArgsConstructor
@NoArgsConstructor
public class B implements Serializable{
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
...
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(/*some join definition*/)
private Set<A> a;
}
Class A:
@Entity
@Audited
@Table
@Data
@Builder
@EqualsAndHashCode(of = "id")
@AllArgsConstructor
@NoArgsConstructor
public class A implements Serializable{
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
...
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(/*some join definition*/)
private Set<User> subscribers;
}
In my controller I have below code:
List<B> bObjects = bService.findAll();
return modelMapper.map(bObjects, new TypeToken<List<BDto>>() {
}.getType());
And the problem is in first run query after database was dropped and recreated. I drop database and run my application, and add new objects of A and B class. And then, when I try to list all objects that are in my table, ModelMapper returns null in place of User class object. Every first run it returns null. When I restart application users are mapped properly. Moreover, when I use UserController to return all users, all users returned corectly and al properties are properly mapped. More itnerresting, that objects of class B contain relationship of class A and these object of class A are returned properly even in first run of Application.
These entities are loaded Eagerly, so hibernate must load it. When I use standard converter to convert A to ADto everything works fine. Converter contains hardcoded line:
aDtoObject.setUserDto(aObject.getUser());
I insert default users in my ApplicationStartUp class.
My hibernate config properties:
hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
hibernate.showSql=false
hibernate.formatSql=false
hibernate.hbm2ddl.auto=update
hibernate.jdbc.batchSize=100
hibernate.orderInserts=true
hibernate.orderUpdates=true
hibernate.jdbc.batchVersionedData=true
hibernate.ejb.event.post-insert = org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener
hibernate.ejb.event.post-update = org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener
hibernate.ejb.event.post-delete = org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener
hibernate.ejb.event.pre-collection-update = org.hibernate.envers.event.AuditEventListener
hibernate.ejb.event.pre-collection-remove = org.hibernate.envers.event.AuditEventListener
hibernate.ejb.event.post-collection-recreate = org.hibernate.envers.event.AuditEventListener
ModelMapper config:
@Configuration
public class ModelMapperConfig {
@Bean
public ModelMapper strictModelMapper() {
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STANDARD);
return modelMapper;
}
}
POM:
<spring-version>4.2.4.RELEASE</spring-version>
<hibernate-version>4.3.8.Final</hibernate-version>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>0.7.5</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-version}</version>
</dependency>
Can anybody help me to figure it out? Any ideas what can I check and what can be a reason to such strange behavior?
P.S. whant to udnerline just with User class. Other classes are working fine.