Java Double Brace Initialization causes IllegalArgumentException: Unknown entity - java

I just try to create a CRUD Web Application with Spring Boot and I found that there is a problem with using Java Double Brace Initialization in the framework.
Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: Unknown entity: com.example.service.impl.FileImageServiceImpl$1; nested exception is java.lang.IllegalArgumentException: Unknown entity:
I have the #Entity class:
#Entity
public class RandomEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
//Getter and Setter
}
A #RestController
#RestController
public class RandomController{
#Autowired
private RandomRepository randomRepository;
#GetMapping("/create")
public String create(){
RandomEntity rdEntity = new RandomEntity(){{
setName("Bla Bla");
}};
return randomRepository.save();
}
}
Here is the repository
public interface RandomRepository extends CrudRepository<RandomEntity, Long> {
}
But when I change Java Double Brace Initialization to Normal Initialization, the Application run properly.
Do you know why is that?
Thank you so much!

It may look like a nifty shortcut that just calls the constructor of your class followed by some initialization methods on the created instance, but what the so-called double-brace initialization really does is create a subclass of your Entity class. Hibernate will no longer know how to deal with that.
So try to avoid it. It has a lot of overhead and gotchas just to save you a few keystrokes.

I just want to complete the answer of #Thilo, If you want a clean code use Builder design pattern, now you can implement this Design easily via Lombok library, so you can Just annotate your Entity like so :
#Entity
#Getter #Setter #NoArgsConstructor #AllArgsConstructor
#Builder(toBuilder = true)
class RandomEntity {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
So there are really some cool annotations, for example #Getter and #Setter to avoid all that getters and setters, #Builder(toBuilder = true) to work with builder design so your controller can look like :
#GetMapping("/create")
public RandomEntity create() {
// Create your Object via Builder design
RandomEntity rdEntity = RandomEntity.builder()
.name("Bla Bla")
.build();
// Note also here save should take your Object and return RandomEntity not a String
return randomRepository.save(rdEntity);
}

Related

Field annotated with #Transient being persisted in #DataJpaTest

I'm practicing TDD,
So now I'm trying to make a test that will fail for now. About to test a #Entity that don't have a field relationship mapped so far. So I'm expecting my test to fail.
Here is TableA entity, you may notice that the TableB relationship is annotated with #Transient, so this field does not get persisted and don't get errors when running other integration tests ( tests that uses #RunWith(SpringRunner.class).
#Builder
#Table(name = "table_a")
#Entity
#Getter
#NoArgsConstructor
#AllArgsConstructor
public class TableAData {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Setter
private String name;
#Transient
#Builder.Default
private List<TableBData> tableBs = List.of();
}
Here is the code for TableB entity, nothing really interesting about it.
#Getter
#Builder
#NoArgsConstructor
#AllArgsConstructor
#Entity
#Table(name = "table_b")
public class TableBData {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Setter
private String name;
}
I also have a TableAJpaRepository, this extends JpaRepository<TableAData, Long>:
public interface TableAJpaRepository extends JpaRepository<TableAData, Long> {
public Optional<TableAData> findByName(String name);
}
My question is:
Why does the follow test is not falling?
#DataJpaTest
#RunWith(SpringRunner.class)
public class TableAJpaRepositoryIntegrationTest {
#Autowired
private TableAJpaRepository repository;
#Test
public void dataFechedByIdWhenGetTableBsShouldBringTableB() {
TableBData tableBItem = TableBData.builder()
.name("123 Test")
.build();
TableAData tableAItem = TableAData.builder()
.name("TableAEntryName")
.tableBs(List.of(tableBItem))
.build();
Long id = repository.save(archetype).getId();
repository.flush();
TableAData fetched = repository.getOne(id);
assertThat(fetched.getTableBs()).isNotEmpty(); // This should be falling
assertThat(fetched.getTableBs().get(0).getName()).isEqualTo("123 Test");
}
}
Looks like getTableBs method is returning the other table entity from relationship, but I don't have it mapped. Am I missing something?
So my friend and I spent some time trying to figure out what was going on. We've found this github issue that describes exactly the same issue. The person who open the issue also create a repo with a minimum reproducible example.
One other thing that helped a lot was this SO answer: You fell in the trap of JPAs first level cache.
Looks like it's because of a cache.
To summarize, the solution was:
Inject TestEntityManager to persist and setup test scenario.
Always using TestEntityManager.persistAndFlush() method.
Call TestEntityManager.clear() before starting tests.
Repositories was used normally in test cases.

How do I create a safe Lombok JPA entity?

I have an #Entity with 20 fields including the index and a timestamp updated by Hibernate:
#Entity
public class MyEntity {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#UpdateTimestamp
private LocalDateTime updatedTime;
private String ....
private String ....
I have a default constructor for Hibernate and a secondary constructor to set everything but the id and updatedTime.
I don't need (or want) setters for id or updatedTime because I only want Hibernate to set them, and it does that with reflection.
I wanted to try out Lombok to see if I could avoid a lot of boilerplate involved here but #Data adds both getters and setters and doesn't create the same constructors.
I'm also concerned that Lomboks generated equals/hashCode and toString methods can cause subtle problems with Hibernate.
This will mean I will have to use a combination of the other Lombok annotations to do this.
How do I safely create an Entity using Lombok like this?
Am I going to have to use a mixture of annotations and manual methods?
Some lombok annotations like #EqualsAndHashCode and #ToString have Exclude option. But neither #Data nor #AllArgsConstructor has a similar option.
But #Data generates setters for all fields for which a setter is not already defined. So you would define a setter as below for the required fields, which does nothing.
private void setId(Long id) {
// Do nothing
}
Instead of the #AllArgsConstructor, you could either use #RequiredArgsConstructor, but annotate all the fields to be in the constructor with #NonNull (or the field should be final).
Refer this answer for RequiredArgsConstructor.
My suggested approach : Another way would be to use #Builder annotation along with #AllArgsConstructor(access = AccessLevel.PRIVATE). (NOTE : Builder by default adds a private all argument constructor, but this is done only if there are no other constructors. But in your case, a default constructor exists and you need to explicitly mention the all args annotation.)
This would prevent the use of the constructor from outside, but at the same time allow you to create objects using the builder. At this point, you could set the values to id and updateTime using the builder. To prevent this you need to add the below code as well.
public static class MyEntityBuilder {
// access is restricted using
// these private dummy methods.
private MyEntityBuilder id(Long id) {
return this;
}
private MyEntityBuilder updateTime(LocalDateTime time) {
return this;
}
}
So, even though it is not possible to achieve your requirement directly, you could do so by adding two dummy setter methods and another two dummy methods within the builder class.
we have #NoArgsConstructor #AllArgsConstructor for generating constructor with lombok.
this is how i create them.
#Entity
#Table(schema = "S25", name = "bank")
#NoArgsConstructor
#AllArgsConstructor
#ToString
#Getter
#Setter
public class Bank {
#Id
#SequenceGenerator(name = "bankEntitySeq", sequenceName = "SEQ_BANKS", allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "bankSeq")
#Column(name = "bank_id")
private Long bankId;
#Column(name = "bank_name")
private String bankName;
#Column(name = "created_on")
private Date createdOn =
new Date(); //Date.from(Instant.now().atZone(ZoneId.of("UTC")).toInstant());
}

Automatically convert Spring JPA Entity OneToMany to List<ID> in DTO and back (ModelMapper)

Animal.java
#Data
#Entity
public class Animal implements MyEntityInterface {
public enum Sex {MALE, FEMALE}
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
private Sex sex;
private boolean castrated;
#OneToMany
private List<Symptom> symptoms;
}
AnimalDTO.java
#Getter
#Setter
public class AnimalDTO implements Serializable {
private long id;
private String name;
private Animal.Sex sex;
private boolean castrated;
private List<Long> symptoms;
}
I wish for a list of Symptoms to be automatically mapped to a list of ID's. This could be achieved in many ways, such as creating a TypeMap, creating a Converter or even just by creating a method in AnimalDTO.java:
public void setSymptoms(List<Symptom> symptoms) {
if (symptoms != null)
this.symptoms = symptoms.stream().map(s -> s.getId()).collect(Collectors.toList());
}
But now imagine it's not only Symptoms, but 50 other fields too. That's a lot of code for the same functionality. And then, it's not only Animal to AnimalDTO, but another 30 different classes with their respective DTOs too.
Also, that still leaves the way back open. From ID to entity. This can (in theory) be achieved easily with the following pseudocode:
List<EntityMemberField.class> list;
for (var entityid : listOfEntityIDsOfDto) {
Object persistedObject = entityManager.find(EntityMemberField.class, entityid);
list.add(persistedObject);
}
...
ModelMapperDestination.setField(list);
This is the same for absolutely every Entity/DTO and should automatically happen for every Entity relationship where the Entity implements MyEntityInterface.
An idea how I could achieve that would be overriding MappingEngineImpl.java from ModelMapper which I register as a Spring Service and inject the EntityManager into, but how could I get ModelMapper to use mine? Or is there maybe an easier way?
The goal is to have a fairly automated conversion from Spring Entities to their corresponding DTO by... just calling modelMapper.map(entity, EntityDTO.class);

How do I use a JPARepository inside of a PrePersist Annotation?

I have an alert table which is transactional and an alert type table which is master. I would like to send out an email whenever an alert is added to the table, so I figured I would use PrePersist. However, in my email, I want to include some information that is included in the alert type table.
I have tried to add a reference to the AlertTypeRepository in the Alert class but I can't because my alert class is a #Table and alertTypeRepository is not a column.
Below is my Alert class
#Entity
#Table
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
public class Alert {
#Id
#GeneratedValue
int id;
#Column
String name;
#Column
String alertTypeId;
#Column
String detailedMessage;
#Column
String status;
#Temporal(TemporalType.TIMESTAMP)
Date time;
}
Below is my AlertType class
#Entity
#Table
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
public class AlertType {
#Id
#GeneratedValue
int id;
#Column
String name;
#Column
String header;
#Column
String footer;
#Column
String summary;
#Column
String deliveryType;
#Column
Boolean active ;
#Column
String recipients;
}
I would like to have a PrePersist function inside of the Alert class. That allows me to access its corresponding header and footer from the AlertType class.
I figured out a solution so I hope this helps anyone facing a similar issue. Basically I had to create an EntityListener to the Alert class and then add the following class.
#Component
public class AlertListener {
static AlertTypeRepository alertTypeRepository;
#Autowired
public void init(AlertTypeRepository alertTypeRepository)
{
this.alertTypeRepository = alertTypeRepository;
}
#PrePersist
public void prePersist(Alert alert) {
List<AlertType> alertType= this.alertTypeRepository.findAll();
}
}
As I know the are two approaches to archive the purpose. Your alterType is not managed by Spring .
Define a JPA EntityListener and apply it on your entity class, which does not seem to interest you.
The second approach, annotated your entity with Spring #Configurable annotation:
#Configurable(preConstruction = true)
class AlterType{
#Inject YourRepository bean as normal.
}
To make it work. Firstly you have to add aspectj related jars into your project dependencies. Secondly you can choose load-time weaving or compile-time weaving to handling the injection for you class.
There is an example of aspectj compiler config in Maven can be used for compile-time weaving(note, just for aspectj compiler maven plugin config, I did not use #Configurable here.).

JPA: dealing with Views

I'm trying to implement a Repository to work with Views. The fact is that I'm trying to use the SimpleJpaRepository implementation, but I'm getting a lot of errors on execution time because my DTO is not an #Entity. It is only a #Table, and it seems that this kind of elements are not mapped into the Metamodel of JPA.
This is my DTO:
#Table(name = "v_language")
public class VLanguage {
#Column(name = "isocode")
private String isocode;
#Column(name = "name")
private String name;
#Column(name = "isdefault")
private String isdefault;
// getters and setters
...
}
I tried to define a customized base repository with minimal functionality (only a findAll() method) with the same implementation of SimpleJpaRepository, but when building the Query it fails when doing:
Root<U> root = query.from(domainClass);
With this exception:
Caused by: java.lang.IllegalArgumentException: Not an entity: class es.prodevelop.pui.common.jpa.model.views.dto.VLanguage
at org.hibernate.jpa.internal.metamodel.MetamodelImpl.entity(MetamodelImpl.java:194)
at org.hibernate.jpa.criteria.QueryStructure.from(QueryStructure.java:124)
at org.hibernate.jpa.criteria.CriteriaQueryImpl.from(CriteriaQueryImpl.java:156)
at es.prodevelop.pui.common.jpa.configuration.AbstractRepository.applySpecificationToCriteria(AbstractRepository.java:213)
at es.prodevelop.pui.common.jpa.configuration.AbstractRepository.getQuery(AbstractRepository.java:174)
at es.prodevelop.pui.common.jpa.configuration.AbstractRepository.findAll(AbstractRepository.java:151)
...
Does anybody know how to solve it?
you are missing the #Entity annotation.
please annotate your class and try again.

Categories