I have an Jhipster app with an existing entity Company.
Now I want to add an Entity Vehicle and created an "vehicle.jh" file for the generator, like this:
entity Vehicle {
name String
category String
// ...
}
relationship OneToMany {
Company{vehicles} to Vehicle{owner}
}
On the first try, the generator complained it does not know about "Company".
I then tried to add it an empty declaration, like this
entity Company
But this overwrites the existing Company-entity classes and removes the existing fields in them.
Is it possible to generate a new entity and connects it with the existing entities, if yes, how? Or should I redoing the steps done the generator by hand, or (or use git to merge them somehow with the original source files)
JHipster requires that all your entities are defined in one single JDL file. If you haven't saved the definition of your previous entities you can export them using jhipster export-jdl app.jh then edit app.jh to add your new entity and re-import using jhipster import-jdl app.jh
Related
AHere's the setup:
Springboot, springboot-data-jpa, hibernate.
I have one entity, a Review, that has a property that has a many-to-one relationship with another entity, a vehicle. The vehicle entity conversely has a one-to-many relationship with the review entity.
To create the review entity via a POST endpoint, I give the vehicle entity as of the properties in the JSON.
This works fine, not only is the review record added in the database but it goes ahead and creates the vehicle entity as well. The only issue is that in the case where the vehicle already exists, instead of making that connection, it creates another record with the exact same information so I have a duplicate vehicle entity.
Is this because I should be handling the creation of the vehicle entity on my own instead of relying on hibernate? Am I just missing some annotation I'm not aware of?
Originally I was getting an error about flushing or something so I added the Cascade.ALL annotation to the review class 'vehicle' property and that fixed that problem. I tried changing the Cascade type as it seems to be relevant somehow but it either breaks the server or doesn't work at all.
Here is the situation, I want to fetch an entity from database and map it to a new view domain model which has more or less properties, if this view model has more properties, signs the extra properties with default value. I want a map technique in JPA to complete this, which is similar to MyBatis mapping mechanism.
So how to do it?
Just load the entity, copy it over in the new entity, fill the unset properties with the desired default values and store it using JPA (possibly via Spring Data JPA).
For copying over the data from one entity to another you might want to look int Dozer or similar libraries.
You could also misuse Spring Data's projection support to query the original entity, but return it as the target entity with methods similar to the following:
interface SourceRepository<Source, Long> extends CrudRepository<Source, Long> {
List<Target> findTargetBy();
}
The resulting Target entities then could be stored again using another repository (you might have to set version and id properties to null to make it clear to the framework that these are new entities.
I´m working on a project using ASPNET Boilerplate where some entities must be versioned, so they have an Id and a versionNumber, where the same entity can have several versions like:
Documents:[
{Document:{id:1, version:1}},
{Docuemnt:{id:1, version:2}}]
So my question is if there is an easy way of doing this, like the implementation of Soft Delete, where I can intercept the update method so it creates a new version.
You can override ApplyAbpConcepts in your DbContext to create a new entity when your Document entity is modified, then reload the original entity so that its changes are not saved.
CancelDeletionForSoftDelete does something similar.
But just doing this won't work because primary key is unique. You can create a composite key.
You will also have to handle relationships (i.e. foreign keys) to avoid linking to multiple versions.
I have created a web site using mvc 3 and Ef code first , now after publishing the site and it's DB I have found out that I need to add a new columns to one of my DB table,
(the DB already has a lot of data in it )
should I add the columns direct to the DB or should I add to the class?
(just a simple string with get and set)
And how can I do it without losing my data in the DB ?
thanks
Adding the columns to the class should be enough. Evidence you can find here.
Here is the full list of changes that migrations can take care of automatically:
Adding a property or class
Nullable columns will be assigned a value of null for any existing rows of data
Non-Nullable columns will be assigned the CLR default for the given data type for any existing rows of data
Renaming a property or class
See ‘Renaming Properties & Classes’ for the additional steps required here
Renaming an underlying column/table without renaming the property/class
(Using data annotations or the fluent API)
Migrations can automatically detect these renames without additional input
Removing a property
See ‘Automatic Migrations with Data Loss’ section for more information
I suggest you to add the columns direct to the DB and to the class, and test it on the local machine.
Do I need to save newly created CRM-entity instances before I can set relations to other crm entity instances?
I'm facing the problem that after calling CrmDataContext.SaveChanges() the newly created entities are written to the database, but the relations between those newly created instances are missing in the database.
What do I miss? Do I have to call CrmDataContext.SaveChanges() each time I create a new crm entity instance that I want to have relations to other CRM-entity instances?
You should be able to set relationships to other entities in a 1:N relationship before saving this entity(i.e. a lookup).
If you are wanting your entity to be referenced by another entity it will need to be Saved first OR you need to set a Guid for the entity first. Otherwise your link won't stick.
When you new up an entity its id is not set until it is saved to the database, unless you set it manually. If you set it manually it will respect the new Guid you have given it and the relationship will survive the saving process.
If you try to save an entity individually, you need to make sure that you have saved all the entities that it refers to before you save that entity, or it won't have a link.