Duplicate inserts when using QueryHints.BATCH - java

I'm developing a Java desktop application (SE, JDK 1.6.0) connecting directly to a remote MySQL Server (5.0) via EclipseLink 2.3.0, using a Swing GUI.
The application where my problem occurs looks like this: the user issues a search for companies (e.g. all companies located in X) and the results are loaded in a JTable. When a user selects a company much additional data from other related tables is shown (e.g. notes about the company, employees, phone number of employees etc.), most of them are #OneToMany relations.
Because of the amount of data and tables needed for each entry I have to optimize my search query to use joins and batch fetch:
import org.eclipse.persistence.config.QueryHints;
import javax.persistence.Query;
Query query = entityManager.createQuery("SELECT DISTINCT c FROM Company c WHERE c.y = x");
query.setHint(QueryHints.LEFT_FETCH, "c.companyWebsiteCollection");
query.setHint(​QueryHints.BATCH, "c.person2companyRelationCollection");
List list = query.getResultList();
The problems occur with person2companyRelationCollection which holds relations to persons who have relations to other companies which have connections to other persons and companies etc.
When I select company1 in jTable1 and all related persons are shown in table2, an INSERT query is issued that tries to insert the shown person2CompanyRelation entity again into the DB, causing a om.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry.
What am I doing wrong with my query hints?
Additional information:
I don't detach any objects
I don't merge any objects
all entities are managed by the same EntityManager
all is done within the same transaction
the PK of Person2companyRelation is a composite primary key (#EmbeddedId) containing
companyId
personId
relationTypeId
the problem doesn't occur when only using with query.setHint(​QueryHints.LEFT_FETCH)
Correction: the problem does occur when batch-fetching any object using QueryHints.BATCH, regardless of the key type

Are you detaching objects, mixing objects from different queries/transaction/entity managers, are you using merge?
Does the insert occur without using batch fetch?
How is the Id of the object and the relationship mapped?

Related

How to implement one-to-many relationships with DynamoDB

everyone! I have two tables that I would like to join via DynamoDb, but since the latter is not a relational db, I don't know how to map the link between the two tables.
In particular, I have a Price List table and a Detail List table that contains the details of the first one. How can I implement one-to-many relationship in java using dynamoDB with Spring Boot?
DynamoDB is basically a key-value store. You only every perform a lookup based on a key. That key may be artificial, not just a user id, but maybe "user_id#product#order" but still it will be a key-based lookup. If you want to use DynamoDB you have to store the data in a way that all queries that you will need will all boil down to basic key-based access (plus some sorting).
You have to do the exact opposite of normalizing your data and splitting relations into multiple tables: you have to de-normalize all your data to store the data and all the relations just in one table, multiple times, with multiple complex artificial keys. See e.g. https://www.youtube.com/watch?v=HaEPXoXVf2k on how to use LSIs, GSIs, how to model your data, how to choose artificial keys, etc.
That means you will not have Item, Order and OrderItem table that you join together, but you will have just one Everything table which may have the fields: userid, username, ordernumber, itemid, itemprice, itemquantity, itemname, orderdate, shippingaddress, etc.
And if you have three items in an order you will have three entries in this table. That means the username will be in the table very often, that means the itemname will be in the table very often and changing them will be difficult but that is how things are if you want to use dynamodb.
That is how you model one-to-many relations, by packing them into a single table and add proper indexes.
If you do have no idea about the current or future access patterns of your data or how to structure your data properly then dynamodb is the wrong tool for you.
The question you are asking gets at the very essence of working with DynamoDB and NoSQL data modeling. It is not as simple as applying your relational database knowledge to DynamoDB. Take a moment to familiarize yourself with the DynamoDB basics before you get too far into solving this problem.
Watch this video about modeling one-to-many relationships in DynamoDB. I would recommend you watch the entire video from the beginning, as it's one of the best introductions to the topic currently available.

Fetch efficiently huge number of entities from DB meeting criteria

Let's say I have about half of million records in table A. Of course table A is joined with table B, C, etc. Now I have to fetch entities from table A which meet my criteria. My criteria consists of about 20-30 rules e.g. name in table A has to be like 'something' or date from table B for joined record from table A with ID=1 should be earlier than today. I see three solutions:
Write native query and put parameters from my criteria. But in this case I will join so many tables that it doesn't seem to me as a good example.
Fetch all records from table A and then check in Java every rule for each record. But this seems for me as the worst possible solution.
Use JPA Criteria. But to be honest I do not know how efficient they are while there are so many records and so many joined tables. What's more it seems to me like working with Criteria can be a little irritating when I have so many rules to match.
Maybe there is another (better) solution to my problem but I cannot see it now. I need to add that I need these fetched entities stored in Java collection because when they are all fetched then I have to work with them (e.g. generate report or create some updates in DB basing on these information).
I hope I described my problem clear and I will be thankful for every tip how to optimize such query.

Retrieve Hibernate associated entity with pagination

I am stuck with an issue. I have 3 tables that are associated with a table in one to many relationship.
An employee may have one or more degrees.
An employee may have one or more departments in past
An employee may have one or more Jobs
I am trying to fetch results using named query in a way that I fetch all the results from Degree table and Department table, but only 5 results from Jobs table. Because I want to apply pagination on Jobs table.
But, all these entities are in User tables as a set. Secondly, I don't want to change mapping file because of other usages of same files and due to some architectural restrictions.
Else in case of mapping I could use BatchSize annotation in mapping file, which I am not willing to do.
The best approach is to write three queries:
userRepository.getDegrees(userId);
userRepository.getDepartments(userId);
userRepository.getJobs(userId, pageIndex);
Spring Data is very useful for pagination, as well as simplifying your data access code.
Hibernate cannot fetch multiple Lists in a single query, and even for Sets, you don't want to run a Cartesian Product. So use queries instead of a single JPQL query.

The correct way to save and query values from a join table in JDO?

I have a Google App Engine web application where I'm using JDO to communicate with a Cloud SQL instance. In my database I have a table "User" and a table "Entity". Any user should be able to "like" one or more entities. So i have created a join table with userID and entityID as composite primary key.
I'm new to all this and I'm wondering how I should approach this with regards to saving and loading of the "likes". When saving a like I would just send the userID and the entityID to the server and store it directly in the join table. When loading, I figured I'd just do a join, but it seems difficult using JDOQL. I've read about M-N-relationship in JDO and wonder if I should model my entity classes with the #Join annotation instead and have a list in both User and in Entity.
It's not clear to me yet how all this works. For fetching, I guess lazy loading is the correct way. So if I understand correctly, when I want to retrieve a list of users that have liked an entity, I can just load the Entity-object and then call getUsers() on the entity while still within the transaction and it will fetch the users mapped in the join table?
If I update an existing Entity with null or empty list for Users, won't any existing relations in the join table then be deleted?

Update all table data at a time in JPA

Is it possible to update all table data in one query?
I have a Database table Person and corresponding Entiry PersonEntity, I can get all Person data vi a JPA in a list such as List personAll.
I have several CRUD operation on personAll instance, I want to reflect all these changes to the Database in one hand using Hibernate JPA
In other words I want content of Person Table is replaced with new content of personAll instance?
Actually long solution way of this question is execute several insert, delete and update operations. But there should be a easy way of doing it?
I can do similar thing when there are two tables Shool Student table if there is OneToMany relation between eash other? Hibernate JPA value removing OneToMany relation
Thanks
It depends on how many rows there are in your table.
If you load all the rows into a Hibernate session and modify the returned instances as required, any changes will be automatically persisted to the database by Hibernate when the session is flushed.
The reason it depends on the size is that if you load the contents of a huge table into a Hibernate session you risk out of memory errors and even if you don't run out of memory the flush will be very slow since every entity in the session much be checked for modifications.

Categories