Error "Duplicate method EnquiryForm() in type EnquiryForm" for #AllArgsConstructor and #NoArgsConstructor - spring-annotations

package cpc.ajinkyaproject.model;
#Setter
#getter
#AllArgsConstructor
#NoArgsConstructor
public class EnquiryForm
{
}
This is the code I have, and I am getting an error. The #AllArgsConstructor and #NoArgsConstructor gives error:
"Duplicate method EnquiryForm() in type EnquiryForm".

This is because you haven't declared any variables in the class. So default constructor and parameterized constructor work/deliver same functionality by JVM.
So declare any variables first and see the difference.

Related

MongoDB Method threw 'org.springframework.data.mapping.MappingException' exception

I have the following model
#Builder
#Data
#Document(collection = "overdue")
public class MGOverdue {
#Id
private String id;
private HashMap<Integer, HashMap<Integer, Overdue>> overdueList;
}
the repository.findById() throws the following exception
Method threw 'org.springframework.data.mapping.MappingException' exception
Caused by: org.springframework.data.mapping.MappingException: No property b found on entity class .<package>.receipt.Overdue to bind constructor parameter to!
Any suggestion please
I had to add #NoArgsConstructor to my model Overdue. It's similar to this one https://stackoverflow.com/a/53210768/7691891

How to have version and audit information inside a nested object in springboot-MongoDB?

I have a version and auditing info in the #document class and it works fine.
Note:using this org.springframework.data.annotation.Version;
EmployeeModelDTO.java
#Getter
#Setter
#Builder
#JsonIgnoreProperties(ignoreUnknown = true)
#JsonInclude(JsonInclude.Include.NON_NULL)
#NoArgsConstructor
#AllArgsConstructor
#Document(collection = "unt-employee")
public class EmployeeModelDTO implements Serializable {
private static final long serialVersionUID = 1L;
private String type;
#Field("group")
private GroupDTO groupDTO;
#Id
#JsonAlias("id")
String id;
#Version
Integer schema_version;
#LastModifiedDate
LocalDateTime updatedDateTime;
#CreatedDate
LocalDateTime createdDateTime;
}
But if i try to put it inside a nested objects it is not getting inserted into mongo db
I need to have it inside GroupDTO.java
#JsonIgnoreProperties(ignoreUnknown = true)
#JsonInclude(JsonInclude.Include.NON_NULL)
#Data
#NoArgsConstructor
#AllArgsConstructor
public class GroupDTO{
#Id
#JsonAlias("id")
String id;
#Version
Integer schema_version;
#LastModifiedDate
LocalDateTime updatedDateTime;
#CreatedDate
LocalDateTime createdDateTime;
}
So if I put the version and other info inside this class, it is not getting populated. Can any one let me know if having the version and other info inside the nested class is possible ?

Why JPQL ignore parent's fields?

App's stack: Hibernate, Spring Data, JPA.
There are some entities in the app. I try make JPQL-query in repository of my class OpenParagraph.
OpenParagraph:
#Entity
#Table(name = "open_paragraphs")
#NoArgsConstructor
#AllArgsConstructor
#Getter
#Setter
#ToString
public class OpenParagraph extends ProgramEntry {
#NotNull
#Column(name = "sort_num")
private Integer sortNum;
}
OpenParagraph has a parent: abstract class ProgramEntry.
ProgramEntry:
#MappedSuperclass
#NoArgsConstructor
#AllArgsConstructor
#Getter
#Setter
#ToString
public abstract class ProgramEntry extends AbstractBaseEntity {
#NotNull
#ManyToOne
#JoinColumn(name = "paragraph_id")
private Paragraph paragraph;
#NotNull
#ManyToOne
#JoinColumn(name = "program_id")
private Program program;
}
So, i tring to appeal to OpenParagraph's field "Paragraph", but IDEA tells me it's mistake:
It doesn't offer me the "program" field:
IDEA offer fields only from OpenParagraph, not from parent.
My question: this is IDEA's fail? If this is'nt IDEA's fail, then how i can call "program" in this query?
This is/was a bug of Intellij IDEA (maybe related to this?). But:
It is possible to query by fields of the super class (or MappedSuperclass). Here is an example:
#MappedSuperclass
#Getter
#Setter
public class Foo extends AbstractPersistable<Long> {
#Column
private String fooValue;
}
#Entity
#Getter
#Setter
public class Bar extends Foo {
#Column
private String barValue;
}
public interface BarRepository extends JpaRepository<Bar, Long> {
#Query("SELECT b FROM Bar b WHERE b.fooValue = ?1")
List<Bar> findByFooValue(String fooValue);
}
Given this, calling the repository method, something like this will be logged (with enabled sql logging):
Hibernate: select bar0_.id as id1_0_, bar0_.foo_value as foo_valu2_0_, bar0_.bar_value as bar_valu3_0_ from bar bar0_ where bar0_.foo_value=?
Hint:
If you are using Spring Boot (with the test dependency/dependencies and an embedded test db like h2), it is quite easy to execute such methods without to run the whole application. Here just a small snipped that would execute the method (even though this is no test, but that's enough to call methods somehow):
#SpringBootTest
public class BarRepositoryTest {
#Autowired
BarRepository barRepository;
#Test
public void testFindByFooValue() {
barRepository.findByFooValue("foo");
}
}

How can I add OneToMany association to an embedded type?

#Entity
#Data
#NoArgsConstructor //
#AllArgsConstructor
#EqualsAndHashCode
public class Decks{
#EmbeddedId
private DecksId decksId;
}
#Embeddable
#NoArgsConstructor
#AllArgsConstructor
#EqualsAndHashCode
public class DecksId implements Serializable{
private Integer id;
#OneToMany(mappedBy = "decksid",cascade = CascadeType.REMOVE)
private List<Deck> deck;
}
How can I make a #OneToMany association from DecksId.class to Deck.class? I have tried many things but none of them has worked. I would like to put many decks into decks.
#Entity
#Data
#NoArgsConstructor
#AllArgsConstructor
#EqualsAndHashCode
public class Deck {
#Id
#GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
#ManyToOne
#JoinColumn
#JsonIgnore
private DecksId decksid;
}
In this case I get the following error code: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepository' defined

Lombok: publicly immutable (over setters) object with all and no arguments constructor, hash, equals and toString

We use Lombok for our entities to generate that common boilerplate, like constructors, hash/equals and toString.
In the same time we'd like to keep our objects immutable. Unfortunately, we can't make the completely immutable (e.g. with final properties) because JPA/Hibernate processors required no-args constructor and sets properties over reflection.
#lombok.Data doesn't fit because it creates public setters
#lombok.Value doesn't fit because it makes properties final and Hibernate can't set them over reflection.
what really fits us is:
#Getter
#AllArgsConstructor
#NoArgsConstructor
#EqualsAndHashCode
#ToString
#Entity
public class Company {
#Id
private int id;
private String name;
}
But this again creates a boilerplate for us, copy-pasting 5 annotations every time and messing the code.
Unfortunately i have not found any way in Lombok to aggregate annotations to some meta-annotation, like in Spring.
Question: is there any out-of-box annotation in Lombok to generate such publicly immutable entities?
Or
is there any way to declare local meta-annotation?
You should be able to use a slightly lighter version:
#Data
#Setter(AccessLevel.NONE)
#Entity
public class Company {
#Id
private int id;
private String name;
}
with the following in lombok.config file:
lombok.noArgsConstructor.extraPrivate = true
I'm not sure whether the extraPrivate configuration works in Lombok 1.18.0. It should, according to the changelog, but I was unable to make it work in a quick attempt.
You may still use #Data annotations, but provide private setters
#Data
#AllArgsConstructor
#NoArgsConstructor
#Entity
public class Company {
#Id
#Setter(AccessLevel.PRIVATE)
private int id;
#Setter(AccessLevel.PRIVATE)
private String name;
}