DTO classes, represent foreign keys on multiple values - mysql

I have a question about DTO class representation. I have two tables on my database, where one has two foreign keys on another one, for example:
book(id, author_name, author_age)
author(name, age, telephone)
where book author_name and author_age are foreign keys on author name and age.
Generally what's the best way to implement this situation on a DTO class?
Inside Book DTO, it's better to do something like:
public class Book {
private String id;
private Author author;
}
or something like
public class Book {
private String id;
private String author_name;
private int author_age;
}
?

DTO are objects that are being used for just transfering the values over communication channel most of time over http or https.
Note : Make your properties as public instead of private.
We should make it as simple as possible.
so use it like
public class Book {
public String id;
public String author_name;
public int author_age;
}
If you would compare the DTO without Author object vs DTO with Author object converted into json or xml, DTO without Author would be less in size.
When using Author inside Book you just add extra wrapper (for author_name and author_age) which cost you some more over communication channel.
in case if Book have multiple authors or in case of one to many relationship you need to do like:
public class Book {
public String id;
public List<Author> authors;
}
Note:But make sure Author should not have a back property with Book instance like following.
public class Author
{
public Book book;
}
in some client side framework like knockout it creates loop with observables.

Related

Is there a way to get "#JsonBackReference" only working when the element is in a collection, but not when standalone?

I am working with fasterxml, and I have two objects that have a relationship "one to many" or "many to one". For example the class Author and the class Book, one author has many books, and one book has one author. Can I tell fasterxml not to serialize the author of the book, only when the book is in it's author's books collection, but when the book is on itself, to serialize it.
Author class:
public class Author{
public int id;
public string name;
#JsonManagedReference
public Set<Book> books;
}
Book class:
public class Book{
public int id;
public string name;
#JsonBackReference
public Author author;
}
That setup works just fine if I want to get only the author, because the books are in place and theirs's author property isn't being serialized, but if I want to serialize only the book, it's author again isn't being serialized, because of the "#JsonBackReference" annotation. Is there any workaround in the said situation? Here are some more examples if you are not getting what I mean...
When I serialize an Autor:
{
id:3,
name: "Ivan Vazov"
books:[
{
id:5,
name: "Under the Yoke"
}
]
}
And that is what I want here.
When I serialize a Book:
{
id:5,
name: "Under the Yoke"
}
But i don't want this, I want this:
{
id:5,
name: "Under the Yoke",
author: {
id:3,
name: "Ivan Vazov"
}
}
Any thoughts on the matter would be great! Thanks.
If you want the references to be serialized from both sides(Book, Author) then jackson faces the issue with circular reference where it get's stuck in an endless loop of references when serializing one of those 2 objects.
The workaround was with #JsonManagedReference and #JsonBackReference where jackson ignored 1 side and serialized only the other side in order to avoid circular reference.
The solution to your problem (when you want to serialize both sides) is to create seperate DTO objects(AuthorDto, BookDto) and instead of returning from your controller a Author to be serialized you return an AuthorDto. Same with Book. Then circlular reference problem does not exist any more and both sides serialize the problematic references.
DTOs are the way to go in more complex scenarios, especially on the inbound side. For dynamic filtering of simpler use cases i wrote an addon for jackson to use antpath style filtering. Probably it helps you:
https://github.com/Antibrumm/jackson-antpathfilter
You have add fetch type in ManyToOne relationship side as follow.
To fetch all book entity then add fetch type Eager.
#ManyToOne(
fetch = FetchType.EAGER,
optional = false
)
To fetch only book entity then add fetch lazy.
#ManyToOne(
fetch = FetchType.LAZY,
optional = false
)

How will you handle versioning if two different json (key) structure point to same pojo?

For example,
I have a POJO like this
class Sample
{
private String name;
}
Second POJO
class Sample
{
private Field field;
}
where the Field POJO contains
class Field
{
private String name;
private int id;
}
I want the name field to support in both versions. Say, for version v2 I want the first POJO to support and for version v2.1 I want the second POJO to support. How to handle this with version-specific change?

Fetch related entities as Base Type

I'm currently trying to setup a database – using Java only. Given this simple class that might appear in the average social network app:
#Entity
class User {
#Id
private String email;
private String name;
private String otherInfo;
#ManyToMany
private List<User> contacts;
}
When the user logs in, he should receive the basic information and the list of contacts with their basic info, but not their contacts. To reduce the amount of boiler-plate code, I want to use a standard solution like Gson. However, even with lazy fetch the whole user is loaded on gson.toJson(user).
Therefore I thought of extracting the basic infos into a base class BasicUser and changing the contacts to List<BasicUser>. Now I only need to somehow circumwent the discriminator column when I fetch the contacts – of course they are all saved as complete users on the server. Unfortunately, I don't know how to archieve that. Any ideas?
If you need to get only part of the entity you can use projections. In your case it can be, for example, like this:
public interface BaseUser {
String getEmail();
String getName();
String getOtherInfo();
}
public interface UserRepo extends JpaRepository <User, String> {
List<BaseUser> findAllBy();
}
Using Jackson for serialization, the problem can be solved without writing custom serialization code. BasicUser contains the getters of the attributes, I want to serialize:
public interface BasicUser {
String getEmail();
String getFirstName();
String getLastName();
}
With a single annotation the contacts attribute is interpreted as a list of BasicUsers:
#Entity
public class User implements BasicUser {
#Id
private String email;
private String firstName;
private String lastName;
#ManyToMany
#JsonSerialize(contentAs = BasicUser.class)
private List<User> contacts = new ArrayList<>();
// ... implemented getters
}
You shouldn't have to modify your domain model just to accomodate a serialization library.
If you only want certain fields of a collection to be exposed to JSON, you could use Jackson with #JsonView (see here: How to serialize using #Jsonview with nested objects) not sure if Gson provides a similar feature as I have never used it extensively.

Pattern to map JPA Objects to DTO for Conversion to JSON

I have a somewhat philosophical question relating to mapping JPA Objects to JSON Strings. Of course there is no necessity for the source object to be a persistent object - it is just that that is my situation.
I have a collection of objects that are managed by Eclipse Link. I need to turn some of these objects into JSON Strings, however the mapping is not one-to-one. I am convinced that the conversion should be loosely coupled so as to isolate the JSON objects from changes in the underlying entities.
I am planning to have the JPA entity as such:
#Entity
#Table(name = "AbnormalFlags")
public class AbnormalFlag implements java.io.Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "Code", unique = false, nullable = false)
private String code;
#Column(name = "Description", unique = false, nullable = false)
private String description;
// Getters and setters
}
and the equivalent object to be converted to JSON
public class AbnormalFlagDTO implements java.io.Serializable {
private String code;
private String description;
private Boolean disabled;
// Getters and setters
}
Is there an elegant pattern or methodology I can use to facilitate this process for several types of objects.
Thanks in anticipation
My answer: no, and also you should generally extend DTOs with care (when reusing existing DTOs). But you could use a Map<String, Object> as a DTO (if you do not use the same DTO to read the data back). Besides you could create an APT (annotation processor tool) that generates the code for DTOs from your entities and then you simply modify them.
This is a perfect use case for Blaze-Persistence Entity Views as you will most probably also want to keep an eye on the performance of the query used for fetching the data.
I created the library to allow easy mapping between JPA models and custom interface defined models. The idea is that you define your target structure the way you like and map attributes(getters) via JPQL expressions to the entity model. Since the attribute name is used as default mapping, you mostly don't need explicit mappings as 80% of the use cases is to have DTOs that are a subset of the entity model.
A mapping for your model could look as simple as the following
#EntityView(AbnormalFlag.class)
interface AbnormalFlagDTO extends Serializable {
String getCode();
String getDescription();
Boolean getDisabled();
}
Querying is a matter of applying the entity view to a query, the simplest being just a query by id.
AbnormalFlagDTO dto = entityViewManager.find(entityManager, AbnormalFlagDTO.class, id);
The serialization of the entity view to JSON will work as expected. If you also want to deserialize objects, you will have to construct the object first and also add setters to the interface.

Is there an easy way to convert System.Data.DataTable to a linq entity?

Is there an easy way to take a DataTable and cast it to its entity type?
I have an Entity called Samples, however through layering this is at one point returned as a DataTable - i woud like to be able to cast this to its entity type if possible?
Thanks
You probably cannot cast, as most probably your entity has no inheritance relation with DataRow (or DataTable, whatever).
Assuming
class Post {
public int Id {get; private set;}
public string Title {get; set;}
public Post(int id) { Id=id;}
}
you'd want something like
foreach (var row in dataTable.Rows) {
posts.Add(new Post(Convert.ToInt32(row[0])) { Title = row[1].ToString()} ) ;
}
not fun, I agree. that's what NHibernate (and the younger brother Entity Framework and it's cousin Linq2SQL ) are for.