Edit JPA-Mapping in IntelliJ Idea - java

I'm having a JPA-Project in IntelliJ Idea and there are some entities my colleague mapped some time ago. Now the DB team added a bunch of tables I'm trying to add as entities to the Java-Project. But when I'm trying to map a new entity to a existing entity IntelliJ Idea doesn't know the entity. So I'm wondering, if the only way is to re-import the table?
BankEntity exists in the JavaProject, but the mapper doesn't recognize it.
Thanks !

If it is an entity that is newly added to the Intellij project, it is unaware of the related table in the database.
You have to Generate Persistence Mapping -> By Database Schema and choose/define the the datasource and then import the table. If the definition of an already mapped entity have been changed(e.g. new column added), then a refresh might help.

I'm going to answer my own question: When generating the entities, Intelli recognizes that there is an existing entity and only add the new attributes to that class. It's somehow confusing, that you have to select the entity like a new entity...but it work's.

Related

How to remove hibernate envers audit history older than 3 months without using native query?

I am using Hibernate eveners to keep the audit data of several entities (Enity_History the audit table) and I use the default REVINFO table.
Is there a way to delete the audit data, that is older than 3 months from today, without using a native query?
Yeah, you should be able to use HQL for this. For every entity with #Audited a new entity is created with the name suffix _AUD. So you should be able to do delete from fully.qualified.name.to.Entity_AUD e where add_days(e.originalId.timestamp, 30*3) < CURRENT_TIMESTAMP

Hibernate auto code-generator from db schema

I'm trying to auto generate class codes with annotation from existing db (mySql).
I have problem with configuration hibernate tools in eclipse this way to dont map foregin ID-key to int but to class.
Eg, I have entity A with ID indicates to entity B (ofc integer), when I use auto generete that tool maps to int, not to class.
How to configure this tool to achive what i mention above?
Thanks,

JPA [Eclipselink] - How to refresh metadata of Dynamic entities created?

I got to know about possibility of Dynamic entity creation in eclipselink from here. And I'm trying to create Dynamic entities and map them to static entities which are already present in the same persistence unit as described in the examples given here.
I'm using refreshMetadata(with empty map of properties) of EntityManagerFactoryImpl to refresh metadata.
But the the dynamic entities are not getting listed in the metamodel of entitymanager factory.
Can somebody let me know where am I going wrong?
I expect they won't, as the Dynamic entity api adds mappings to the native EclipseLink session, while the JPA metamodel is build from JPA mappings. refreshMetadata is used to rebuild the native EclipseLink session using any new JPA metadata (orm.xml etc), but does not go the other way.
I was able to refresh the metamodel by adding a new metamodel with the current session by the following code snippet:
Metamodel metamodel = new MetamodelImpl((AbstractSession) dynamicHelper.getSession());
((EntityManagerFactoryImpl) emf).setMetamodel(metamodel);
Though this didn't solved my main problem, it solved the problem I've asked here.

How to persist entities using collection in JPA

I'm trying to persist entities in OneToMany relationship using JPA. I have two entities, Proyecto (Project) and Participacion (Participation). Proyecto can have multiple Participacion. The problem is when i try to persist a Participacion to an existing Proyecto.
//First, I get the selected project by the user from the database.
Proyecto proyecto = proyectoManager.getProyecto(Integer.parseInt((String) session.getAttribute("idProyecto")));
//Second, I create and set the Participacion parameters, including the project.
Participacion participacion = new Participacion();
participacion.setIdProyecto(proyecto);
participacion.setLogin(usuario);
participacion.setPorcParti(Integer.parseInt(request.getParameter("porc")));
//Finally I persist participacion with his respective project
proyectoManager.addParticipacion(participacion);
//Here, I'm trying to get the project to check if his Participacion collection contains the new participation.
proyecto = proyectoManager.getProyecto(Integer.parseInt((String) session.getAttribute("idProyecto")));
The problem is in the second time I recover the project from database, because his participation collection doesn't contain the new participation. I've tried using edit() method and even flush after persist() method but it doesn't work.
The next time I deploy the Enterprise Application, the project contains his respective participations, including the new one, but I need it after persist the first time, without deploying again.
Can any one help me? Thank you.
How do you tell JPA that you want to cascade persist the collection?
I mainly use annotations on my domain objects, and usually just add something like:
#OneToMany(cascade=CascadeType.ALL)
This (or similar) question has been answered many times on this site.
Edit
Also, from the code sample above, it seems as you are not adding the Participation instance to the Project. I would think that you need something like:
proyecto.addParticipation(participation);
proyectoManager.saveProyecto(proyecto);
to complete the association.

HIBERNATE: Auto append word into TABLE

hi I have an entity with this annotation
#Entity
#Table(name = "REPORT_WORK")
But for some reason hibernate keep saying Missing Table: REPORT_REPORT_WORK
I know that is the problem because of when I change the name to "REPORT_WORKX"
It will say Missing Table: REPORT_REPORT_WORKX
Has any encountered this issue before?
Update: when I change the name to JJJJ
It will say Missing Table: REPORT_JJJJ
so for some reason there it is auto appending REPORT_
Configuration:
hibernate.hbm2ddl.auto=validate
I suspect that the problem is your Hibernate configurations. Specifically, if you don't have an appropriate setting for hibernate.hbm2ddl.auto, Hibernate won't automatically update the database schema when you change your model.
(And if you don't want the updates to happen automatically, then you need to figure out what schema changes are needed, code them as SQL DDL, and run them manually.)
Can you post your persistence.xml (or equivalent)?
It sounds like you are implementing org.hibernate.cfg.NamingStrategy, get rid of this configuration.
Some additional info:
JPA (Hibernate) and custom table prefixes

Categories