I'm interested in getting the "Description" property of a table column. Is it possible to do via Hibernate? I'm using Sql Server 2008.
Edit: I'm trying to map the column description found in information_schema to the corresponding column (JPA column entity)
I think the simplest solution would be mapping of informatio_schema in way that will provide the information for columns. If you are using annotations you easily can access to information like Table name and Column name, that can be used in Criteria to fetch the description.
This is probably re-coding some available feature and non platform depend. But if you will not succeed in research is always some solution.
Related
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.
I've started working on a legacy oracle database and using Spring boot Jpa trying to insert a new row in a table (CHANNELMGR_REQUEST) without Identity:
This table has a Numeric column (CM_ISN) and logically could be an Identity candidate, but I could not touch the database for some reason.
I found an existing sequence (CHANNELMGR_SEQ) that is used for generating value for that CM_ISN column too.
So I decided to use that sequence and added some annotation in my equivalent POJO as follow and mapped to the sequence to that CM_ISN column. But not touching the database :
My repository is like this :
While inserting the row, sucessfully invoke the sequence but get exception as bellow :
My questions :
Is it wrong to modify an entity and add #Id to that which is not in in equivalent table?
What's wrong with my code that i get error?
PS: I'm sorry for putting images instead of actual source codes, The reason is because development machine has no access to the internet.
It's wrong if it is not the primary key
Use BigDecimal instead of Number
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.
My application is using OpenJPA to connect with a Postgres database. In the schema I am using the inet postgres datatype in a column. This field in Java is a String. I am able to read the field correctly, but I am having problems inserting a new row.
Searching on the Internet I have found three possible solutions to do this:
Creating a Native query. This method works, but in my specific case creating a Native query in order to insert this row implies creating more queries that were being managed by OpenJPA which can lead to lots of bugs. So it is not the more suitable solution in this case.
Creating a PostgresDictionary like in this question: How to use Postgres inet data type with OpenJPA?. I have implemented this exactly how this user explains. I have created the custom PostgresDictionary, I have added the columnDefinition in the #Column annotation and I have added the property in the persistence.xml. But my custom PostgresDictionary is never called.
When the application created the PostgresDictionary keeps creating the org.apache.openjpa.jdbc.sql.PostgresDictionary instead of the custom one.
Implementing a custom Strategy, like this example http://webspherepersistence.blogspot.co.at/2009/04/custom-orm-with-openjpa.html. But in order to implement the Strategy, I have to set the type of the column from the class java.sql.Types (http://docs.oracle.com/javase/6/docs/api/java/sql/Types.html?is-external=true) and there is no inet type in this column. I tried Types.OTHER, but I still have the same error indicating that the column is a type inet and the value I am trying to insert is varchar (String).
So, does anybody has an idea how to fix the problem I am having with the mapping?
The solution in the point 2 was not working because the openjpa.jdbc.DBDictionary was been overiden by the class org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter that had the database property set to POSTGRESQL. Which aparently set the DBDictionary to org.apache.openjpa.jdbc.sql.PostgresDictionary independently of the value set in the persistence.xml property.
Deleting this database property from the OpenJpaVendorAdapter allowed me to use my custom PostgresDictionary.
I have implemented a system that uses the ZQL library to parse sql statements and retrieve column names and table names.
Now I have got an additional requirement that requires me to find which column belongs to which table.
If anyone has previous experience of this either in ZQL library or some other SQL parsing library in java .Please respond
Immediate idea: take the results from you first implementation (parsing table and column names) and feed the data into a Map, where the column name is the key and the value is a set of table names (for a general approach, a column name may be used in more then one table)
Then you can do a get(columnName) and the map will give you the name(s) of the table(s).