How to have version and audit information inside a nested object in springboot-MongoDB? - 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 ?

Related

spring data JPA OneToMany query pagination of child (many) table

I have the following code. The number of rows in the One table will be about 100 and the number of rows in the Many table will be several hundred thousand (millions). I'm going to need to paginate the Many rows per single row in One when I query.
I'm not sure how to accomplish this.
My first thought is to get only the rows (and NOT the manyList content) in One but not even sure of that in JPA OneManyRepository. And then for each row use it's id primary key to query the Many with that one_id value and do pagination that way.
Any help will be apprecited.
Thanks,Jim
public interface OneManyRepository extends JpaRepository<One, Long> {
#Query(value = "select one.id,one.columnOne,one.columnTwo from one", nativeQuery = true)
Optional<List<One2>> findAllOne();
#Query(value = "select many.someTimeValue,many.value from many where many.one_id = :id", nativeQuery = true)
Optional<List<Many>> findAllBy(#Param("id") long id);
}
#Builder
#AllArgsConstructor
#NoArgsConstructor
public class One2 {
private Long id;
private String columnOne;
private String columnTwo;
}
#Entity
#Table(name = "one")
#Data
#Builder
#AllArgsConstructor
#NoArgsConstructor
public class One {
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
private Long id;
private String columnOne;
private String columnTwo;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "one")
private List<Many> manyList;
}
#Entity
#Table(name = "many")
#Data
#Builder
#AllArgsConstructor
#NoArgsConstructor
public class Many {
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Id
private Long id;
#ManyToOne
private One one;
private long someTimeValue;
#Column(length = 10)
private String value;
}

Why JPA update entity causes stackoverflow?

I have an entity Task with id. Tasks belongs to Config. I need to update Task with it's Config doesn't change. Here is my code:
Task:
#Entity
#Getter
#Setter
public class Task{
#ManyToOne
#JoinColumn(name = "config_id", referencedColumnName = "id")
private Config config;
#OneToMany(orphanRemoval = true,cascade = CascadeType.ALL)
#JoinColumn(name="task_id")
private Set<ActivityItemTask> activityItemTasks = new HashSet<>();
}
#Entity
public class ActivityItemTask {
private Double score;
#EmbeddedId
private ActivityItemTaskId activityItemTaskId;
#Getter
#Setter
#Embeddable
#NoArgsConstructor
#AllArgsConstructor
public static class ActivityItemTaskId implements Serializable {
#ManyToOne
#JoinColumn(name = "activity_item_id")
private ActivityItem activityItem;
#ManyToOne
#JoinColumn(name = "task_id")
private Task task;
#ManyToOne
#JoinColumn(name = "config_id")
private TaskConfig config;
}
}
Config:
#Entity
#Getter
#Setter
public class Config{
#OneToMany(mappedBy = "config")
private Set<Task> tasks = new HashSet<>();
}
TaskService:
#Service
public class TaskService{
#Resource
TaskRepository taskRepository;
#Transactional
public Long save(Taskdto dto){
Config config = new Config();
config.setId(task.getConfigId());
s.setTaskConfig(config);
return taskRepository.save(s).getId();
}
}
TaskDto:
#Data
public class TaskDto {
private Long id;
#NotNull
private Long configId;
private String name;
private Date beginDate;
private Date endDate;
private String note;
}
when TaskService#save was called , it throw StackOverflowException:
org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError
the log shows that my application querys task record and querys task's config and config's tasks and so on.
I am wondering what's wrong with my association annation. Any advice are appreciated.
I'm sorry.I have written another 2 calss so that I can find out the truth. It turns out my third calss ActivityItemTask may be the root cause. I think Task. activityItemTasks may should be annnotation with mappedBy=? But which field should be writtern here?
I did config wrong association.
Task should be :
#OneToMany(mappedBy = "activityItemTaskId.task")
// #JoinColumn(name = "task_id")
private Set<ActivityItemTask> activityItemTasks = new HashSet<>();
This is how to use mappedBy annotation with embedable class.
Thank all you guys commented or answered. You did help me find it out.

Cannot access Postgres database with Hibernate

I was already searching for a solution but I cannot find any.
My problem is that I cannot my Postgres database, called IncomeOutgo, via Hibernate.
I always get this error message when calling my Thymeleaf/HTML website.
So does my application.properties look like
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://192.168.0.227:5432/IncomeOutgo
spring.datasource.username=postgres
spring.datasource.password=XXX
#spring.jpa.properties.hibernate.format_sql=true
So does my table look like in Postgres (DBeaver):
And last but not least so does my Domain/Entity look like:
#Data
#NoArgsConstructor
#AllArgsConstructor
#Builder
#Entity
#Table
public class IncomeOutgo extends AbstractPersistable<Long> {
#Version
#Column(name ="id")
private Long id;
#Column(name="dayofweek")
private Date dayofweek;
#Size(min = 5, max = 50)
#Column(name ="person")
private String person;
#Min(0)
#Column(name ="version")
private Integer version;
#Column(name="income")
private int income;
#Column(name="outgo")
private int outgo;
}
Maybe, someone can tell me what my error is?
Thanks in advance!
I was able to make it work now.
Lesiak´s hint with the schema name made me do a workaround.
I created a new database: incomeoutgo (lower letter case), and under schema: public, a new table: incomeoutgo with lower letters, too.
And so does my Domain definition look like:
#Data
#NoArgsConstructor
#AllArgsConstructor
#Builder
#Entity
#Table(name="incomeoutgo", schema = "public")
public class IncomeOutgo extends AbstractPersistable<Long> {
#Version
#NotNull
#Column(name ="id")
private Long id;
#Column(name="dayofweek")
private Date dayofweek;
#Size(min = 5, max = 50)
#Column(name ="person")
private String person;
#Min(0)
#Column(name ="version")
private Integer version;
#Column(name="income")
private int income;
#Column(name="outgo")
private int outgo;
}

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

How can I load only specific child object to a mapping place holder

I have following object mapping in our project. One contact can have multiple quotes from typeA and typeB.
#Entity
#Table(name = "t_ind_contact")
public class Contact implements Serializable {
#Id
#Column(name = "id", unique = true)
private Long id;
#OneToMany
private List<Quote> quotes;
}
Abstract Quote class.
#Entity
#Inheritance
#DiscriminatorColumn(name = "app_code")
#Table(name = "t_ind_quote")
public abstract class Quote implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name = "id")
private long id;
#OneToMany
#JoinColumn(name = "contact_id")
private Contact contact;
#Column(name = "app_code", insertable = false, updatable = false)
private int appCode;
}
TypeA Quote class
#Entity
#DiscriminatorValue("1")
public class TypeAQuote implements Serializable {
private static final long serialVersionUID = 1L;
}
TypeB Quote class
#Entity
#DiscriminatorValue("2")
public class TypeBQuote implements Serializable {
private static final long serialVersionUID = 1L;
}
This mapping is working fine for all operations. But for some operations we need the contact object with typeA quotes only. As of now, we will filter typeA quotes after system loads data from the database.
Is there a way that we can populate a contact object only with typeA quotes? I mean even the generated queries will load only typeA quotes.