I have 6 different tables and the first table has a child table and the second table is the parent for the second one(First to second tables all one to many relationships). So this continues to 6 tables. Something like the main table would be college and child will be Student and then for Student we have Subject as child tables and for Subject, we have chapters to as a child.
Can some help me with an example of how can I map this multiple join query to a BO using AlisastoBaean transformer. The reason I am going with this approach is that
1) I have these tables used everywhere and cannot use lazy or eager loading
2) I just require two or 3 columns form each table.
3) And few columns have clob data type.
Related
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.
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 relation between the entities must be changed and I'd like to know is it normal to change the association mapping type and whether the data that already exist in db will be transfered normally? I tried to find information about it but didn't found. Or if the mapping will be changed the data that already exists must be transfered manually via sql queries? Thanks
If the association already uses a join table, and the mapping of the many-to-many keeps using the same join table with the same column names, you won't have to do anything except removing the unicity constraint you could have on one of the ci=olumns of the join table.
Otherwise, yes, obviously, you'll have to migrate your schema, using SQL, or any other tool (FlywayDB, Liquibase, etc.).
I could do it simply:
Let A and B be the original tables.
A->B (N-1) was moved to A<-AB->B (N-N).
I had to
- "remove" the foreignkey column from table A,
in favor of records to be inserted into AB
(made of the two foreign keys leading to A and B)
That's all.
Step one: Replace your Many-to-One annotation by your Many-to-Many annotations.
and lauch hibernate in append mode to generate the N-N table
Step two: Insert record in this N-N table given what is found in the remaining foreign-key column of you 1-N relationship.
Step three: Delete this foreign-key column.
In Database, I have a customer entity and a event entity. There is a MANY-TO-MANY relation between them called reserve.
reserve has three columns, CUSTOMER_ID, EVENT_ID and SEAT.
The first two columns are primary keys of each entity. The third one is a relational attribute that's set specifically for the relation, so it is ONLY in the relation table.
What I am struggling is should I create a separate class for the reserve or should I do some trick in the two existing two classes to make this third one exist?
Thanks.
A many-to-many relation table must have only two attributes: the two primary keys of the tables that should be linked. In your case, since you store another attribute within that relation table, it's not a pure relation table and you have to create a extra POJO for it. As far as I know.
I am using Spring-hibernate Framework and Oracle database. Now there is a table named project. each record has got an unique id,name and a parent_id column. Say the table has got these following records...
ID NAME PARENT_ID
1 A NULL
2 B 1
3 C NULL
4 D 3
5 E 3
now i have create a tree view where these records will be displayed in according to their Parent-Child relationship. Like This..
1---P
ch---2
3---P
ch--4
ch--5
Now can anyone help me how to create the tree dynamically fetching records from the database dynamically. And what will be the Data Access Object(DAO) and the Implementation of the DAO..
any help or suggestion will be very helpful.
First select only PARENT_ID. Then for select everything where ID is IN the previous array of results (http://www.w3schools.com/sql/sql_in.asp).
From this you can then create parent in the tree and then lazy load children on expand.
Or you can populate tree with children immidiately. You just select all where PARENT_ID is NOT NULL and attach them to appropriate parent (by PARENT_ID).
I'm not sure what DAO has to do with it. But in case you just want some abstraction you can use DAO pattern.
DAO could have methods like: getAllParents, getChildrenForParentId, getAllChildren, ...
Just made self table reference in POM (.java class) with List type one to many annotation.
#OneToMany(fetch = FetchType.EAGER)
#JoinColumn(name = "PARENT_ID",nullable=true)
#OrderBy(clause = "NAME")
#ForeignKey(name = "fk_child_project")
private List<Project> projects= new ArrayList<Project>();
What you need is a Oracle Hierarchical query. Since you are using Oracle DB, you are in luck.
But the only way this can done thru Hibernate is by using a native query and mapping the results back to Objects.
Too many questions, I'll try to steer you in the right direction -- this might not drop you to your destination.
Create your Entities, See the docs
You may not need a DAO, just create a business class to query the database. (Hint: use EntityManager in case you are using Hibernate's JPA implementation), See the docs
Then display it as a tree in your browser, assuming you are working on a webapp
Note: You can come again and ask specific questions regarding how, as a separate question.
Related thread
How to implement self join in JPA