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.
Related
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
CrudRepository#save doesn't allow you to use default columns. null-fields of an entity are interpreted as NULL not DEFAULT.
If I use a custom #Query("INSERT INTO ... DEFAULT ..."), then I'm unable to obtain the ID of the inserted row.
There is currently no build in way of using the default values from the database.
While #Jay's answer isn't aimed at Spring Data JDBC, the approach of setting the attributes to their default value in the constructor does work with Spring Data JDBC.
The alternative would be to implement a custom method which does the insert and retrieves the default values back from the database.
AFAIK not all databases support more then one return value from an insert so you might have to actually reselect the data written to the database.
I'm writing a Java application that uses PostgreSQL as its RDBMS back end. I'm using Hibernate to create the DB schema (actually I already created the db schema myself but I got rid of that because I wanted to make sure my app would be able to understand it properly...).
I'd like to use some of Java 8's new types - specifically, the Duration type, and I'd like this to map to a column of type Interval. However, when Hibernate generates my schema it creates the column with type bytea, which is not going to be useful when I try to load in data from CSV files. A column of type interval, on the other hand, is able to read time formats like 01:23:45 from csv files and persist it just fine.
So how can I tell Hibernate that this Java field of type Duration should be mapped to a column of type Interval?
Thanks.
Edit: Note that this is not a question of how do I hook my app up to hibernate and postgresql. I already have it hooked up and configured for the most part. I am asking how to define custom type mappings so that my java Duration gets mapped to a PostgreSQL interval rather than a byeta.
I am currently trying to set the Storage Engine for a table, because from case to case MyISAM and InnoDB should be used. Unfortunately I did not find a way to set this in Hibernate, and I do not want to create each table by hand. My prefered way would be to anotate it in the Java-POJO, but I couldn't find a way to do so.
I've found Hibernate: what's the difference between MySQLDialect and MySQLInnoDBDialect?, which tells me that setting the Dialect to org.hibernate.dialect.MySQLMyISAMDialect would help (it says this for the InnoDB-Dialect, but it seems like one could choose the default dialect with this), but this method has two shortcommings: First, the dialect is chosen for all tables, and second, it creates a query like CREATE TABLE xy(..) type=MyISAM but the query would be correct with engine=MyISAM.
Also Hibernate mysql innodb says that there are defaults, and that I can overwrite them when creating tables, but not how - does somebody know how to do this?
Instead of org.hibernate.dialect.MySQLMyISAMDialect or org.hibernate.dialect.MySQLInnoDBDialect, you should use org.hibernate.dialect.MySQL5MyISAMDialect or org.hibernate.dialect.MySQL5InnoDBDialect.
This way, it creates the table using engine=MyISAM or engine=InnoDB.
But this configuration is global and if you really have a reason to configure it for a single table, you could try:
Hibernate import SQL (hibernate.hbm2ddl.import_files). Add the path to a SQL file which contains the alter table into it.
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.