Mapping one-to-many relationship using QueryDSL SQL - java

Lets say I have this two bean entities:
public class Audit {
private String code;
private java.sql.Timestamp creationDate;
private String creatorId;
private java.sql.Timestamp deletionDate;
private String description;
private String id;
private String name;
private String notes;
private Short status;
private List<AuditParticipant> participants;
}
and :
public class AuditParticipant {
private String auditId;
private String department;
private String id;
private String name;
private String notes;
private String role;
private String surname;
}
... where Audit can have 1..n Participants, how can I use QueryDSL SQL to project the list of the participants into Audit bean (get all participants that belongs to audit)?
The beans were generated using QueryDSL code generation.
Thanks

Querydsl provides result aggregation functionality for such cases http://www.querydsl.com/static/querydsl/3.1.1/reference/html/ch03s02.html#d0e1634
In this case it would be something like
query.from(audit)
.innerJoin(participant).on(...)
.transform(groupBy(audit.id).as(audit, list(participant)));
See these examples for other groupBy options https://github.com/mysema/querydsl/blob/master/querydsl-collections/src/test/java/com/mysema/query/collections/GroupByTest.java

Related

Is this method of fetching user details for posts in my application correct?

so im making a twitter-like app using a Spring Boot backend.
When returning Tweet DTOs from the back end I want it so the tweet also shows the user profile picture, username and id.
My approach was to create a Dto called AuthoInfoDto which is part of the Tweet Dto
AuthorInfoDto:
#Data
#Builder
public class AuthorInfoDto {
private String username;
private Long authorId;
private String profileImageUrl;
}
TweetDto:
#Data
#Builder
public class TweetDto {
private Long id;
#NotBlank
private String title;
#NotBlank
private String content;
private BigInteger likes;
#NotBlank
private Instant date;
#NotNull
private AuthorInfoDto authorInfo;
}

java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not:

I'm trying to get all records from collection.Below is code for reference.
public class ParkingPlace{
#Id
private String id;
private String placeId;
#DateTimeFormat(iso = ISO.DATE_TIME)
private Date creationDate;
#DBRef
private List<Review> placeReviews;
// Getters and setters
}
public class Review{
#Id
private String id;
#DBRef
private Author reviewAuthor;
//getters and setters
}
public class Author{
#Id
private String id;
private String fullName;
//getters and setters
}
public class ParkingPlaceDAO{
#Autowired
MongoOperations mongoOperations;
public List<ParkingPlace> getAllPlaces() {
// new mongodb query
Query query = new Query();
// sort by date
query.with(new Sort(Sort.Direction.DESC, "creationDate"));
// run query
return mongoOperations.find(query, ParkingPlace.class);
}
}
So in controller when I call getAllPlaces() method, it throws
java.lang.IllegalArgumentException: BasicBSONList can only work with numeric keys, not: [reviewAuthor]
I've checked database and all collections and also fired query from mongo shell It gives me result.But not via code

How to put propper DBFLow annotation

I want to insert doctor object to database, how should I put annotations for properties?
I tried to do it with te code shown below.
But i don't know how to do it on list properties specializations and phoneNumbers.
#Table(databaseName = WMDatabase.NAME)
public class Doctor extends BaseModel{
#Column
#PrimaryKey
#Unique(unique = true)
private String doctorId;
#Column
private FullName fullName;
#Column
private String organizationId;
#Column What shuld i put here?????
private List<Specialization> specializations;
#Column What shuld i put here?????
private Contacts contacts;
}
Below are the classes I use for doctor attributes:
public class Contacts extends BaseModel {
private List<PhoneNumber> phoneNumbers;
private String email;
private String fax;
}
public class Specialization extends BaseModel {
#Column
#PrimaryKey
#Unique(unique = true)
private String doctorId;
#Unique(unique = true)
private String specializationName;
public String getSpecializationName() {
return specializationName;
}
public void setSpecializationName(String specializationName) {
this.specializationName = specializationName;
}
DBFlow is a relational database system (not a mongo-type key/value store) and doesn't support lists as columns, according to the doc here.
List : List columns are not supported and not generally proper for a relational database. However, you can get away with a non-generic List column via a TypeConverter. But again, avoid this if you can.
The documentation on relationships may help you refine the model to suit your needs.

Trying a best solution?

I'm looking for a solution to my problem. I have 2 java class of domain. Graduacao and Aluno, in Graduacao I have an attribute "graus" that is a Collection.
In Aluno class, I have collection attribute "List graduacao".
I add the Graduacao in a JComboBox about ComboBoxModel and when user selected a Graduacao I have a JList that show the "graus" of Graduacao.
What I need is get Graduacao and graus that user choose and add to Aluno and persist after show results in a JTable with AbstractTableModel.
I'm trying this
#Entity
#Table(name="graduacao")
public class Graduacao {
#Id #GeneratedValue
private Integer id;
#NotNull #Column(unique = true)
private String graduacao;
#ElementCollection
#CollectionTable(name="graduacao_grau", joinColumns=#JoinColumn(name="id_graduacao"))
#Column(name="grau")
private List<String> graus;
//get and set
#Entity
#Table(name="aluno")
public class Aluno {
#Id #GeneratedValue
private Integer id;
//informacoes gerais
#NotNull
private String nome;
private String cpf;
private String rg;
private String nomePai;
private String nomeMae;
#Temporal(TemporalType.DATE)
private Date dtNascimento;
#Temporal(TemporalType.TIMESTAMP)
private Date dtCadastro;
private String status;
private String observacoes;
//logradouro
private String endereco;
private String bairro;
private String complemento;
private String cidade;
private String cep;
#Enumerated(EnumType.STRING)
private EstadoBrasileiro uf;
//contato
#ElementCollection
#CollectionTable(name="telefone_aluno", joinColumns=#JoinColumn(name="id_aluno"))
#Column(name="telefone")
private List<String> telefones;
private String email;
//graduacao
#OneToMany #JoinColumn(name="id_aluno")
private List<Graduacao> graduacao;
#Temporal(TemporalType.DATE)
private Date dataGraduou;
//federacao
#OneToMany #JoinColumn(name="id_federacao")
private List<Federacao> federacao;
//get and set
here the print
/** edit */
I solved the problem, here the project: http://www.4shared.com/zip/1Gbj-IZLce/project_example.html
A complete example is beyond the scope of StackOverflow. Two approaches are common, although neither is simple:
Create a custom TableModel that uses JPA queries and entities to implement the methods required by AbstractTableModel; a very simple example using JComboBox is shown here; a complete TableModel example with pagination is shown here.
Use org.jdesktop.beansbinding, shown here and mentioned here and here.
A number of ancillary links related to this topic are shown in this answer.

org.hibernate.ObjectNotFoundException issue with using list()

The following query throws the exception:
Query query = session.createQuery("from Associate as a order by a.username asc");
associates = query.list();
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [ca.mypkg.model.Associate#0]
If I create an entry in the database with id of 0 it works just fine. I don't really get it because I'm just trying to load all the entries in the db not just a specific one.
Similar questions I've found have been concerned with trying to load an object with a given ID I'm doing no such thing.
Associate class:
#Table(name = "user")
#XmlRootElement(name = "associate")
public class Associate implements Serializable {
private String username;
private String password;
private String firstName;
private String lastName;
private String userType;
private int id;
private String email;
private String isActive;
private Department dept;
private String lastUpdated;
private String associate_type;
// ...
#Id
#GeneratedValue
public int getId() {
return id;
}
#OneToOne
#JoinColumn(name = "dept")
public Department getDept() {
return dept;
}
From my experience this type of error message usually means it does not find joined entity by mentioned id, and not the entity requested in the query (Associate, in your case).
My guess is that Associate class contains a join entity which has primitive type primary key.

Categories

Resources