How can I create collection OneToMany without using annotations?
Now I'm using DatabaseFieldConfig like this:
DatabaseFieldConfig collection = new DatabaseFieldConfig("name");
collection.setForeignCollection(true);
But it doesn't work because I always get "Not fields has DatabaseField annotation" when I'm trying to create Dao.
Below is my example code:
public class HasCollection {
private long id;
private Collection<CollElement> coll;
...
}
public class CollElement {
private long id;
private HasCollection parent;
...
}
When I'm creating DAO for HasCollection I get error that "Not fields has DatabaseField annotation in CollElement" and if I'm creating DAO for CollElement I get error "Not fields has DatabaseField annotation in HasCollection"
I made a sample code when extending class but it would drag dependency on ormlite DAO into this extended model class which is definitly not the right pattern.
Therefore in this case I would consider the class from another project as a DTO (data transfer object) and I would create another model class with annotations for ormlite.
Than made a simple convertor from DTO to the model class and vice versa used in layer where you want to persist objects.
Related
I've heard when you want to return some object from a service method, you have to define a DTO object (or POJO object generated with JSON Schema) instead of using an Entity.
To make it clear, here is the example:
We have an entity and a jpa repository for it:
#Data
#Entity
#Table(name = "tables")
public class Table {
#Id
private Long id;
private String brand;
}
This is a bad practice:
#Service
public class MyService {
#Autowired
private TableRepository tableRepository;
#Transactional
public Table create() {
Table table = new Table();
// Some logic for creating and saving table
return table;
}
}
This is a good practice:
#Service
public class MyService {
#Autowired
private TableRepository tableRepository;
#Transactional
public TableDTO create() {
Table table = new Table();
// Some logic for creating and saving table
// Logic for converting Table object to TableDTO object
return tableDTO;
}
}
Why is this so?
Thank you!
Probably you mean a DTO (Data Transfer Object), not DAO (Data Access Object). Let me clarify this:
Data Transfer Object:
A Pojo that represents a piece of information. Usually it has aggregated data in it.
Data Access Object:
An object that performs access to some kind of persistence storage for retrieving information, someone considers it a synonim of Repository, someone not.
Entity:
An object that represents data that has been retrieved from the database.
Why is returning an Entity from the Service considered a bad practice?
The reason is that the Entity is something that is very close to the database. It contains primary key, someone could guess your database structure from it and the set of the data in case of query can be verbose. Hence, it is preferable to have some kind of logic, usually a mapper, that hides primary key and aggregates data to be less verbose and to not expose the db structure. Also, while the Entity is built on the table structure, the DTO can be customized in base of caller needs. Usually it contains exactly the data that is needed for some action and nothing more than this. Suppose you have thirdy party software that calls your backend services: you should not expose the db structure (Entities) to this service. It is better to define a contract, with the minimal information needed for this thirdy party service to operate, and expose only this part of the information, hiding all the rest.
Hope that's a little bit more clear now.
Edit:
Of course there are other good reasons for using DTOs instead of Entities, this is only an introductory explanation to the subject.
It's about passing interface of DTO to DAO.
For example I have following code
public interface User {
String getName();
}
public class SimpleUser implements User {
protected String name;
public SimpleUser(String name) {
this.name = name;
}
#Override
public String getName() {
return name;
}
}
// Mapped by Hibernate
public class PersistentUser extends SimpleUser {
private Long id;
// Constructor
// Getters for id and name
// Setters for id and name
}
I'm using generic DAO. Is it ok if I create DAO with using interface User instead PersistentUser?
User user = new PersistentUser(name);
UserDao.create(user);
I read a lot of topics on stack but not figured out is this approach ok or no. Please help me. Maybe this is stupid and I can achive only problems.
About separating beans.
I did this because some classes I want to share via API module, that can be used outside to create entities and pass them to my application. Because they uses interface I developed so I can pass them to my DAO for persisting.
Generally, I would say it is ok, but there are a few hidden problems. A developer could cast the object down or access some state via a toString method that shouldn't be accessible. If you don't be careful, it could happen that state is serialized as JSON/XML in webservices that shouldn't be serialized. The list goes on.
I created Blaze-Persistence Entity Views for exactly that use case. You essentially define DTOs for JPA entities as interfaces and apply them on a query. It supports mapping nested DTOs, collection etc., essentially everything you'd expect and on top of that, it will improve your query performance as it will generate queries fetching just the data that you actually require for the DTOs.
The entity views for your example could look like this
#EntityView(PersistentUser.class)
interface User {
String getName();
}
Querying could look like this
List<User> dtos = entityViewManager.applySetting(
EntityViewSetting.create(User.class),
criteriaBuilderFactory.create(em, PersistentUser.class)
).getResultList();
I have a normal model class
public class Person {
private int id;
private String name;
...
}
It's a model class and doesn't have any JPA / Hibernate annotations used.
Is it possible to somehow tell Hibernate to make this class persistent?
I want to use Person in queries, criteria etc, but don't want to introduce annotations to that class (it is defined in a model JAR with no JPA dependency, and I have the DB code in a different JAR);
Hibernate supports XML mappings to map a class to a database, as well as annotations.
Say I have the following Java class, which is owned by a vendor so I can't change it:
public class Entry {
private String user;
private String city;
// ...
// About 10 other fields
// ...
// Getters, setters, etc.
}
I would like to persist it to a table, using JPA 2.0 (OpenJPA implementation). I cannot annotate this class (as it is not mine), so I'm using orm.xml to do that.
I'm creating a table containing a column per field, plus another column called ID. Then, I'm creating a sequence for it.
My question is: is it at all possible to tell JPA that the ID that I would like to use for this entity doesn't even exist as a member attribute in the Entry class? How do I go about creating a JPA entity that will allow me to persist instances of this class?
EDIT
I am aware of the strategy of extending the class and adding an ID property it. However, I'm looking for a solution that doesn't involve extending this class, because I need this solution to also be applicable for the case when it's not only one class that I have to persist, but a collection of interlinked classes - none of which has any ID property. In such a scenario, extending doesn't work out.
Eventually, I ended up doing the following:
public class EntryWrapper {
#Id
private long id;
#Embedded
private Entry entry;
}
So, I am indeed wrapping the entity but differently from the way that had been suggested. As the Entry class is vendor-provided, I did all its ORM work in an orm.xml file. When persisting, I persist EntryWrapper.
I don't have much experience with JPA, but I wouldn't extend your base classes, instead I would wrap them:
public class PersistMe<T> {
#Id
private long id;
private T objToWrap;
public(T objToWrap) {
this.objToWrap = objToWrap;
}
}
I can't test it, if it doesn't work let me know so I can delete the answer.
I'm working on a JPA project. I have an ExportProfile object:
#Entity
public class ExportProfile{
#Id
#GeneratedValue
private int id;
private String name;
private ExtractionType type;
//...
}
ExtractionType is an interface implemented by several classes, each for a different extraction type, these classes are singletons.
So type is a reference to a singleton object. I don't have an ExtractionType table in my DB, but i have to persist the extraction type of my export profile.
How can I persist the ExportProfile object using JPA, saving the reference to type object?
NOTE: The number of ExtractionType implementations is not defined, because new implementation can be added anytime. I'm also using Spring, can this help?
Here's an idea: make an ExtractionTypeEnum, an enumeration with one element for each of the possible singletons that implement ExtractionType, and store it as a field in your entity, instead of ExtractionType . Later on, if you need to to retrieve the singleton corresponding to a ExtractionTypeEnum value, you can implement a factory that returns the correct singleton for each case:
public ExtractionType getType(ExportProfile profile) {
switch (profile.getExtractionTypeEnum()) {
case ExtractionTypeEnum.TYPE1:
return ConcreteExtractionType1.getInstance();
case ExtractionTypeEnum.TYPE2:
return ConcreteExtractionType2.getInstance();
}
}
In the above, I'm assuming that both ConcreteExtractionType1 and ConcreteExtractionType2 implement ExtractionType.