How to generate POJO from PostgreSQl/PostGIS? - java

I have a 25 .xsd schemas and want to generate DB tables and POJO classes from this schemas.
Hibernate can do it? Im already try generate DB tables with XMLSpy and POJO with JiBX, but when i generate mapping with Hibernate names of tables in DB and in mappings was different. I want to use single tool to do this.
UPDATE
I try generate POJO with hibernate. I create \hibernate.cfg.xml ,hibernate.reveng.xml and console config. And when i try run configuration get error
org.hibernate.MappingException: Could not configure overrides from file: D:\liferay-develop\workspace\JABX_test\src\hibernate.reveng.xml
Could not configure overrides from file: D:\liferay-develop\workspace\JABX_test\src\hibernate.reveng.xml
org.hibernate.MappingException: invalid override definition
invalid override definition
org.hibernate.MappingException: invalid override definition
invalid override definition
org.xml.sax.SAXParseException: The content of element type "hibernate-reverse-engineering" must match "(schema-selection*,type-mapping?,table-filter*,table*)".
The content of element type "hibernate-reverse-engineering" must match "(schema-selection*,type-mapping?,table-filter*,table*)".
When i use empty TableFilter in reverse enginering hibernate create POJO but only from deafult postgres tables.
what i doing wrong?
UPDATE 2
How i use this tutorial http://www.youtube.com/watch?v=tfZiKn5xhXk and get error
org.hibernate.exception.JDBCConnectionException: Getting database metadata
Getting database metadata
java.sql.SQLException: No suitable driver found for jdbc:postgresql:localhost:5632:postgis_test
No suitable driver found for jdbc:postgresql:localhost:5632:postgis_test
java.sql.SQLException: No suitable driver found for jdbc:postgresql:localhost:5632:postgis_test
No suitable driver found for jdbc:postgresql:localhost:5632:postgis_test
UPDATE 3
Okey. Im reinstal eclipse and get situation like in first UPDATE again. So i try generate POJO from DB and get only POJO from postgis tables: geography_columns and spatial_ref_sys. Hibernate cant work with PostGis?

Related

Apache Ignite - how to evolve schema/properties of an annotated class

I'm using Apache Ignite with class annotations as described in "Query Configuration by Annotations".
How should we handle class changes? For example what happen if from v1 and v2 of my application I add a new property?
Are previous values deserialized? Can I specify a default value?
I cannot find any documentation on this topic. I have tried with a simple use case and seems that new properties are null. How can I handle this?
UPDATE
Following suggestions from #dmagda I have tried to add a property on my class, adding it to the table using ALTER TABLE MYTABLE ADD COLUMN myNewProperty varchar; and then changing it's value using UPDATE MYTABLE SET myNewProperty='myDefaultValue'.
But unfortunately running the abode UPDATE I get the exception: Error: class org.apache.ignite.binary.BinaryObjectException: Failed to unmarshal object with optimized marshaller (state=50000,code=0)
It is possible to update existing records by changing new fields using SQL? How?
UPDATE 2
Solved my problem. It was caused by the fact that my class was written in scala with some scala specific types ('Map', ...). My app connects to Ignite using client mode and so when executing UPDATE from sqlline utility Ignite was unable to deserialize the types.
Now I switched my class to be plain POJO and now I'm able to update schema and update data.
Just update your Java class by adding a new field and it will be stored and can be read back without any issue. You might see null as a value of the new field for two reasons:
It was not set to any specific value by your application
You're reading back from Ignite an old object which was stored before you updated your class and, thus, the new field didn't present there.
If you need to access the new field using SQL, then use ALTER TABLE command to add the field to the SQL schema.

How to use Hibernate 5.2.10 MySQL JSON support without AttributeConverter or customUserType to map to Java Entity Class?

I am trying to map the MySQL JSON column to Java Entity class. Looking for the cleanest way of doing this.
Upon doing some research found 3 possible ways:
Extending AbstractSingleColumnStandardBasicType
Create a custom UserType
Use an attribute Converter
I used an attribute converter to convert the JSON column from String (as MySQL driver makes it to a String) to my required type - this works with both the Hibernate V4.3.10 and V5.2.10
I tried to find if JSON is natively supported in Hibernate and found the PR https://github.com/hibernate/hibernate-orm/pull/1395, based on the PR looks like it does add JSON mapping to the MySQL Dialect hence letting Hibernate know about the JSON Column.
Does this mean I can use something like this to map to JSON Column in DB ?#Column(name="json_type_column")
Private Object correspondingJsonAttribute;
If I cannot use it like this and need to use one of the above 3 methods, is there a reason I would need to upgrade to get the registerColumnType( Types.JAVA_OBJECT, "json" ); which is part of the PR and is present in Hibernate V5.2.10, Do I get any more features from V5.2.10 that support JSON columns?
I also looked into the corresponding test case to understand how the JSON column mapping is being done https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/access/MixedAccessTestTask.java, this uses #Access annotation via property, looks like it sets the corresponding JSON column variable in Entity to Map after converting it from String.
Any help is much appreciated.
Thanks!
Upon doing some research found 3 possible ways:
Extending AbstractSingleColumnStandardBasicType
Create a custom UserType
Use an attribute Converter
AttributeConvertor won't help you for this, but you can still use a custom UserType, or Hibernate Type Descriptors.
Does this mean I can use something like this to map to JSON Column in
DB?
#Column(name="json_type_column") Private Object
correspondingJsonAttribute;
No. The json type is just for JDBC so that Hibernate knows how to handle that JDBC object when setting a parameter on a PreparedStatement or when fetching a ResultSet.
Do I get any more features from V5.2.10 that support JSON columns?
No, but you just need to supply your own JSON type.
You can just use the hibernate-types which is available on Maven Central.
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>${hibernate-types.version}</version>
</dependency>
And use the provided JdonType from Hibernate Types as it works on MySQL, PostgreSQL, Oracle, SQL Server or H2 without doing any modifications.

Spring data cross-store with MongoDB and MySQL

I am trying to create a data access layer where an application would like to use both a traditional SQL store as well as a NoSQL store. After few searches, I have found a concept like Spring Data MongoDB cross strore and this concept similar to my requirement. So that, I have successfully configured as suggested in the reference document https://stackoverflow.com/a/13998825.
But I am getting below error when I test the above sample. Can some one please help in this regard...
Error: Caused by: org.hibernate.MappingException: Could not determine type for: com.hcc.riab.model.ProductInfo, at table: Product, for columns: [org.hibernate.mapping.Column(productInfo)]

How to Solve SQL Integrity Constraint Violation Exception

I created an entity class that has the same properties as project.java, and created a class where I can persist the entity object. Also, I created a database using databases in Netbeans using embedded JDBC. I have the persistence.xml, which provides the properties to connect the db, and is used the persitence class on entitymanagerfactory object. The connection seems fine but I am having Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'PROJECT_ID' cannot accept a NULL value. error.
Is it ok to create the db manually (executing the ddl) or should I create the table in the persistence.xml using property name="javax.persistence.jdbc.url"" value="create-tables ?
Regards
You have to set the value of the column PROJECT_ID.
You can either do this in your code, for example by using the annotations #SequenceGenerator or #GenericGenerator,
or use db features (trigger) to set the id during insert.

Mapping a boolean[] PostgreSql column with Hibernate

I have a column in a PostgreSql database that is defined with type boolean[]. I wish to map this to a Java entity property using Hibernate 3.3.x. However, I cannot find a suitable Java type that Hibernate is happy to map to. I thought that the java.lang.Boolean[] would be the obvious choice, but Hibernate complains:
Caused by: org.hibernate.HibernateException:
Wrong column type in schema.table for column mycolumn. Found: _bool, expected: bytea
at org.hibernate.mapping.Table.validateColumns(Table.java:284)
at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1130)
I have also tried the following property types without success:
java.lang.String
java.lang.boolean[]
java.lang.Byte[]
How can I map this column?
You need to implement your own UserType. This article is very handy. I am including this link because the link in the original article is broken.
You can implement your customer user types by overriding some open source UserTypes

Categories

Resources