How many tables can be mapped in hibernate mapping.? - java

I am new to hibernate and am using xml file for mapping. I want to map more than 8 tables, is it possible or not ?
I am using mysql for DB. In all that mapping examples they use maximum 3 tables in mapping. If I change the value in one table that has to reflect it on to next one.
For Example I am giving the Username in my Employee Table and use that one in my login table. If I am mapping 2 tables it means, the username in employee table can automatically update to my login table. Is this correct ?
And how many tables am I able to map in single xml file.

A hibernate component may be your solution. Also hibernate definitions can be in separate files and hibernate will figure it out.
https://www.google.com/search?q=hibernate+components

Related

Retrieve table hierarchie from Hibernate

Now and then I come into the situation that I have to display the table hierarchie of a database for further operations, currently in a data migration project where I have to treat "leaf tables" (tables which are leafes in the table dependency tree) in a different way.
I've always wanted to use Hibernate's meta information to retrieve and display the table dependency tree, but never knew how to approach the problem.
So can anyone give me feedback on whether Hibernate provides an API to do this? I am not asking for a complete solution, the information if there is an API and what it is called is absolutely sufficient.
I want to solve the following questions:
Which tables are in the database?
Is a given table a root table (not dependant from other tables)?
Is a given table a leaf table (dependant from other tables but no table is dependant from the given table)?
Which tables are dependant from the given table?
On which tables does the given table depend?
I know how to retrieve the mapping between entities and tables:
How to discover fully qualified table column from Hibernate MetadataSources , but I want the relationship between the tables.
In a custom MetadataContributor you can access metadataCollector.getDatabase() which exposes the full relational model to you. You just have so save that into a static volatile variable and then access it later on in your app to do whatever you want to do with it.

Should I use mySQL Table Views or Spring Data JPA projections?

I have a mySQL relational database with football statistics that contains a table matches. I created a method in my Spring project to build a standings table. This method uses a projection because I need each match object to include the two team objects. This response (get all matches + get the two teams in each match) takes around 7 seconds.
The same information but within a View in my database takes 0.231 seconds.
I'm very new to Spring Data so my question is. Should I use table views when I need to join tables? Is there any advice against doing so?
I don't see any problem with using table views. You can map to them with JPA #Table annotation.
The only potential problem is when migrating databases (you will have to make sure Views are migrated correctly).
Hope this helps.

ADD/DELETE column from xml mapping

I have some problem with hibernate. when I add a column age into the file mapping of Client.xml, hibernate update my table client and add the column, but when a delete the same column age from Client.xml and client.java, and I run my application, I found that the column still in my table client.
can anyone have a clue why hibernate couldn't delete the column age from table client
thank you ^_^
Hibernate follow two approach
1. Schema first
2. Code first
In Schema first Java entities are created based on your database design and in Code first approach database is created with all java entities annotated with #Entity.
In your case you are following Code first approach, when you add any new column in java entity hibernate will create column on your behalf if hibernate.hbm2ddl.auto is update. if your database contains more tables hibernate has no issue with it that's why hibernate is not deleting any column if you delete it from Java Entity.

Excist any Hibernate auto Update Mapping

is there any method to support my mapping anyways in Hibernate?
I mean to correct my mapping automatically, when changs in Database were made?
The best would be that hibernate woud conform to DB, so DB is leader and Hibernate only maps and dont make any changes.
Example:
Yesterday a Table named "Phones" had the columns:
id
Number
Today that table got a Update and now it looks like:
id
Number
customerID
PhoneListId
is there any option that hibernate correct this automatically?

Is it possible to ask Hibernate to create table from .hbm.xml file?

I want to develop a DB structure where every "object" will go in its own table. Say, objects are "house", "car", etc. The structure of the tables is the same for each object. The tables are named this way: common prefix and the name of object. Also every object has two associate tables that go in one-to-many relationship.
For this purpose I created a naming strategy that returns the name of the physical table based on the name of the object.
The problem is that I can receive an object that has no table so I need to create it. I tried with direct SQL query and it worked fine before adding some foreign relationships to the table. Now I receive the following exception:
Exception in thread "main" org.hibernate.DuplicateMappingException: Table [INV_OBJECT_ATTRIBS] contains logical column name [id] referenced by
multiple physical column names: [INV_ATTRIB_ID], [INV_KEY_ID]
My question is:
Is it possible to ask Hibernate from within code to create a table based on .hbm.xml file? Something like session->createTableFromXML(...)?
Yes, look at the Hibernate schema generation tools hbm2ddl:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/toolsetguide.html

Categories

Resources