JHipster relation between predefined table USER and my own table - java

I need to define new table related to predefined 'User' table.
Please, help me to write a correct JDL code for this
entity diagram
I tried to write file "mytable.jh" and import :>jhipster import-jdl mytable.jh
entity MyTable{
userid Long, //relation to table jhi_user
}
relationship OneToMany {
User{id} to Mytable{userid}
}
and got
{ name: 'IllegalAssociationException',
message: 'Relationships from User entity is not supported in the declaration between User and Mytable.',
prototype:
Error

Basically you can't add new fields to the jhi_user table, and JDL won't let you do anything that would cause that to happen.
You can add a User object reference to another entity with something like
relationship ManyToOne {
Mytable{userid} to User
}
Note there that I am not putting {anything} after User -- this means the User has no idea which Mytable it's associated with. If you want to "back up" from an instance of Mytable to an instance of User, you have to search from the Mytable side; the User won't have any fields or getters/setters related to an instance of Mytable.
Also note that the userid field is not necessarily strictly for relating the two entities. The JDL and code generation actually takes care of creating the relationships and primary/foreign keys for you. So
entity Car {
}
relationship ManyToOne {
Car{user} to User
}
means for every User, there are many Cars, or another way of saying it is for every Car there is one user whose field name within the Car object will be user. So you'll have Car.getUser() method, etc.
When you define a field within an entity block, you're defining a property of that entity, not a relation -- that property is independent of all other objects in your model.
You still won't have a User.getCars() method though. This is not possible with current jHipster design.

I think this tip in the jhipster doc might help you, I never looked at it, but a mate did this during my project I'm crrently working on. Hope this helps. You can always ask Paul-Etienne for further information, he will gladly help.

Related

Updating a single entity without affecting its dependencies in Hibernate

Consider the following scenario:
I have a Person entity that has a many-to-many relationship with Group entity and a many-to-many relationship with Role entity. Both entities have bidirectional relationship with Person setting CascadeType to SAVE_UPDATE. The FetchType is set to LAZY.
Person also has attributes like name, birthdate, id, etc.
Since I'm working in a heavy transactional multithreaded environment, as a design consideration, when a certain Person attribute is going to be modified, the Person object retrieved through hibernate session's get method is being copied using the following method:
public Person personCopyWithoutDependencies(Person entity){
Person person = new Person();
person.setId(entity.getId());
person.setName(entity.getName());
person.setBirthdate(entity.getBirthdate());
person.setGroups(null);
person.setRoles(null);
return copy;
}
Then, I do Person's specific attribute modification in the copy object and then call the Hibernate session's Update method. The copy is done to prevent issues with multiple sessions in multiple threads (when doing a modification sessionFactory.openSession() is called and closed after commit while when retrieving an objtect sessionFactory.getCurrentSession() is being called).
If the person object that I modified had previously associated Group and Role entities, it deletes the relationship in the database.
I want to know if it's possible to do the modification I'm intending without losing its relationships.
Thank you very much in advance. Any advice is welcome.
The only way to do this, is to validate, whats changed, and retrieve REal Hibernate object back.
Steps:
You do have a copy with some attributes. (NULL instead of Proxy lazy collections)
You do change some of them.
You retrieve Real object again from Hibernate (DB)
You map objects attributes back to real object, and just do not do this for NULL attributes.
commit changes.
This is the only way to do this. As #Gimbly mentioned, you are trying to handle, whats Hibernate suppose to do.

Hibernate: update entry with changed type

I'm dealing with CRUD on a class hierarhy: Employee->{Manager, Waiter, Chef} using Spring MVC.
I'm using single table inheritance strategy with a field DTYPE as a discriminator.
Where I am stuck is a situation, when I need to update Waiter -> Manager (not is a case a waiter has been promoted, but just to fix a user's input error!!!) or any other combination of entities.
Hibernate in itself refuses to change the type recorded in DTYPE field, even if I explicitly do:
managerDAO.update(manager);
where manager is an object of Manager class with already existing Id (belongning to a Waiter type (DTYPE = "Waiter")) in the DB.
Now the question is: How do I correct my user's input mistake which is seemingly pretty simple and prevalent situation? I just need to make former Waiter entry become a Manager entry with the same ID.
I wonder if Hibernate has some tricks to deal with a situation like this? Because I do not want to re-invent a wheel OR find a workaround feeling this should be foreseen by the Hibernate team.
I have already considered
SQL update on DTYPE field and
delete and re-insert a new entry of the right type... but this all looks like a wrong usage of Hibernate.
Any help would be appreciated!
Try to query the "waiter" object casted to a "person" object (assuming person is the parent in your inheritance tree), then cast it a manger, set relevant fields and persist the manger object.

Netbeans/JPA change owning side of many to many relationship

I'm using netbeans "Entity classes from database" function to generate my entity classes. Sadly, netbeans chooses the wrong manytomany relationship owner. I have for example a user and group table which are related via many to many. In my case the group entity is the owner with #JoinTable and User has the "MappedBy" annotation. How can I make User to the owning entity. I could change it manually but after generating it again from database, it will be overwritten. This is really annoying. Any ideas?
Regards, Manu

How to make a bidirectional one-to-one relation by GreenDAO?

I use ORM GreenDAO for DAL layer of my Android app. I've a problem in one to one relation between two tables. for example:
Entity page = schema.addEntity("Page");
Entity bookmark = schema.addEntity("Bookmark");
Property pageIdProperty = bookmark.addLongProperty("pageId").getProperty();
bookmark.addToOne(page, pageIdProperty); // any bookmark related to a page
page.addToOne(bookmark, pageIdProperty); // any page can has a bookmark
Is this relation mistake?
When I define it, I can't call page.getBookmark() in the Page Class
If this is true, Does any way I can make it bidirectional for one to one relationship?
First off, good use cases for one-to-one relations are rare. Are you sure that it's not a one-to-many? If you still think it's a one-to-one: does it make sense to merge both entities into one?
Besides that, bookmark.addToOne(page, pageIdProperty) is fine, while page.addToOne(bookmark, pageIdProperty) does not work. To-one relations require the id property to be in the same entity. To-many would work this way, check the documentation on relation for details.
Best workaround so far is to use the addToOne on the entity where the foreign key exists and addToMany on the other side. It is not terribly inconvenient as you only have to make sure that you add one object to the list.
You have several errors. First you need to create a PK before FK property on bookmark.
Try this:
Entity page = schema.addEntity("Page");
Entity bookmark = schema.addEntity("Bookmark");
bookmark.addLongProperty("bookmarkId").primaryKey().autoincrement();
Property fkPage = bookmark.addLongProperty("pageId").getProperty();
bookmark.addToOne(page, fkPage).setName("page");
Do the same steps for Page entity.

What are JPA entities?

I am starting to use JPA and I always get confused with the term of entities and their usage, I have read a lot but I still don't quite get it.
I read the Oracle documentation of it but it does not really explain its role in the transaction.
What are JPA enities? does they actually hold the data for each row, I mean, are they stored instances that hold the row data? or they just map tables of the db and then insert and delete in them?
for example if I use this:
entity.setUserName("michel");
Then persisting it, then changing the user name, and persisitig it again (i.e merging it)
Does this change the previously entered user name? or does it create a new row in the db?
An Entity is roughly the same thing as an instance of a class when you are thinking from a code perspective or a row in a table (basically) when you are thinking from a database perspective.
So, it's essentially a persisted / persistable instance of a class. Changing values on it works just like changing values on any other class instance. The difference is that you can persist those changes and, in general, the current state of the class instance (entity) will overwrite the values the row for that instance (entity) had in the database, based on the primary key in the database matching the "id" or similar field in the class instance (entity).
There are exceptions to this behavior, of course, but this is true in general.
It's a model. It's a domain object that can be persisted. Don't over think it. Akin to a Rails model. And remember, models (in this paradigm) are mutable!

Categories