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
Related
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.
I recently upgraded Spring Boot and with this came a hibernate upgrade. Unfortunately, the entity column #Type(StringClobType) annotation has been deprecated. The documentation tell me I need to switch it to MaterilizedClobType.
Unfortunately this has broken my application.
I'm using PostgreSQL 9.5. The StringClobType annotation created a text type in the database which allowed me to store long text in the field. Unfortunately now, the string literal comes back when Hibernate is expecting some kind of LOB id.
This gives the error: Bad value for type long
The Hibernate #Type value that maps to PG's Text data type is org.hibernate.type.TextType. This is what you should use.
For what it's worth, this a sibling of org.hibernate.type.MaterializedClobType, which maps to CLOB; both are subclasses of org.hibernate.type.AbstractLongStringType.
I try:
#OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
protected List<URL> urlList;
But I get error:
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'entityManagerFactory' defined in ServletContext resource
[/WEB-INF/servlet-context.xml]: Invocation of init method failed; nested
exception is org.hibernate.AnnotationException: Use of #OneToMany or
#ManyToMany targeting an unmapped class:
my.package.MyModel.urlList[java.net.URL]
I saw this answers, but I can't mapping list of URLs.
Using an ElementCollection and CollectionTable is the way to map a Collection of NON-ENTITY elements.
#ElementCollection
#CollectionTable(name="MY_URLS")
Collection<URL> urlList;
Then it would depend on whether your JPA provider supports persistence of that element type (URL) out of the box. My provider (DataNucleus) does. You always can use AttributeConverter if it doesn't
What version of Hibernate are you using? Also, in your database, what is the datatype of the column; is it varchar, or something else?
From what I am understanding, it looks like your issue is that Hibernate does not know how to translate the column in the database to the java.net.URL class, a Hibernate type would need to be defined to tell Hibernate how to handle this conversion.
It looks like Hibernate does have a built-in type that should handle this conversion, assuming you are using a version of Hibernate that has it -- 3.6 or greater, I think.
Try adding this annotation to your List:
#Type(type="org.hibernate.type.UrlType")
If your version of Hibernate does not have org.hibernate.type.UrlType or if your database column data type is not a varchar, you will need to create a custom Hibernate type that defines what Hibernate needs to do to convert whatever data type in the database to a java.net.URL and back. Let us know if it gets to that point and we can give you more information.
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?
I have recently changed an object to have a #OneToMany mapping to another object, with the FetchType.LAZY. But when I try to load a list of these objects using a #NamedNativeQuery, which calls an Oracle function, it throws a java.sql.SQLException: Invalid column name for this new OneToMany mapping. But being marked as LAZY, it shouldn't try to populate this variable should it?
In theory I could change the function to return an empty value for this column (basically a hack), but I would have to roll that out to everywhere that uses a #NamedNativeQuery to populate one of these objects.
This seems like a bug to me. Is there a workaround, something I'm missing or possibly fixed in a later version of Hibernate?
I'm using hibernate-core 3.3.2.GA, hibernate-entitymanager 3.4.0.GA, hibernate-annotations 3.4.0.GA and hibernate-commons-annotations 3.3.0.ga.