I am using hibernate entity and a sequence generator with it.
Database is Postgres and hibernate version is - 5.4.24.Final.
I have a table X with a sequence "seq_x" and below is the hibernate model for it.
#Audited
#Entity
#Table(name = "x")
public class XModel {
private Long id;
private String strCol;
#Id
#Column(name = "id_gen", columnDefinition = "serial")
#GeneratedValue(strategy= GenerationType.IDENTITY)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Column(name = "str_col")
public String getStrCol() {
return strCol;
}
public void setStrCol(String strCol) {
this.strCol = strCol;
}
}
Now the problem occurs when inserting a value in table X using hibernate model.
Sometimes the insertion fails as i have not explicitly specified the sequence name "seq_x" using #GenericGenerator and hibernate was looking for a default sequence name "x_id_gen_seq". That seems to be a valid behaviour. In this case i see the below query in logs throwing exception:
select currval('x_id_gen_seq')
Now Sometimes, the insertion works and in logs i see
"2022-06-13 11:44:25,431 DEBUG [org.hibernate.id.IdentifierGeneratorHelper] (default task-116) Natively generated identity: 523"
Now, i am literally confused what could be the reason behind it. I tried going through the hibernate source code but could not find much.
I have a model which i want to save to database.
#Data
public class Model {
#Id
private UUID id;
private String name;
private ModelSettings settings;
#Data
static class ModelSettings {
boolean fuelEngine;
}
}
create table model
(
id uuid not null,
name varchar(25) not null,
settings jsonb
)
i try to save modelSettings as jsonb object using simple repository method save(), but i got error
ERROR: relation "settings" does not exist
i wrote custom Converter and i see when modelSettings is converted to json, but after prepare statement Spring Data try to save settings field to related table. How to tell Spring Data save field as json only, not row in related table?
Sorry, i forgot #WritingConverter with JdbcValue.
Hi Please use Embedded Annotation:
Embedded entities are used to have value objects in your java data model, even if there is only one table in your database.
#Data
public class Model {
#Id
private UUID id;
private String name;
#Embedded(onEmpty = USE_NULL)
private ModelSettings settings;
#Data
static class ModelSettings {
boolean fuelEngine;
}
}
You can not have an object in your entity and expect JDBC to save it as json for you.
you need to define a String column and write a converter for it for saving in and reading from database.
also some databases like Oracle supports json values but you have not mentioned which database you are using.
in Oracle database you can define a table including a json column as below:
CREATE TABLE "USERS"
(
"ID" NUMBER(16,0) PRIMARY KEY,
"USER_NAME" VARCHAR2(85) UNIQUE NOT NULL,
"PASSWORD" VARCHAR2(48) NOT NULL,
"PROFILE" NCLOB NOT NULL CONSTRAINT profile_json CHECK ("PROFILE" IS JSON),
"SETTINGS" NCLOB NOT NULL CONSTRAINT settings_json CHECK ("SETTINGS" IS JSON),
);
And you need to create your entity class as below:
#Entity
#Table(name = "Users")
public class User {
#Id
#Column(name = "ID")
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USER_GENERATOR")
#SequenceGenerator(name = "USER_GENERATOR", sequenceName = "USERS_SEQ", allocationSize = 1)
private Long id;
#Column(name = "USER_NAME")
private String userName;
#Column(name = "PASSWORD")
private String password;
#Lob
#Nationalized
#Column(name = "PROFILE",columnDefinition="NCLOB NOT NULL")
private String profile;
#Lob
#Nationalized
#Column(name = "SETTINGS",columnDefinition="NCLOB NOT NULL")
private String settings;
}
as you can see here profile and setting are my json columns.
I use in application MySQL 5.7 and I have JSON columns. When I try running my integration tests don't work because the H2 database can't create the table. This is the error:
2016-09-21 16:35:29.729 ERROR 10981 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000389: Unsuccessful: create table payment_transaction (id bigint generated by default as identity, creation_date timestamp not null, payload json, period integer, public_id varchar(255) not null, state varchar(255) not null, subscription_id_zuora varchar(255), type varchar(255) not null, user_id bigint not null, primary key (id))
2016-09-21 16:35:29.730 ERROR 10981 --- [ main] org.hibernate.tool.hbm2ddl.SchemaExport : Unknown data type: "JSON"; SQL statement:
This is the entity class.
#Table(name = "payment_transaction")
public class PaymentTransaction extends DomainObject implements Serializable {
#Convert(converter = JpaPayloadConverter.class)
#Column(name = "payload", insertable = true, updatable = true, nullable = true, columnDefinition = "json")
private Payload payload;
public Payload getPayload() {
return payload;
}
public void setPayload(Payload payload) {
this.payload = payload;
}
}
And the subclass:
public class Payload implements Serializable {
private Long userId;
private SubscriptionType type;
private String paymentId;
private List<String> ratePlanId;
private Integer period;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public SubscriptionType getType() {
return type;
}
public void setType(SubscriptionType type) {
this.type = type;
}
public String getPaymentId() {
return paymentId;
}
public void setPaymentId(String paymentId) {
this.paymentId = paymentId;
}
public List<String> getRatePlanId() {
return ratePlanId;
}
public void setRatePlanId(List<String> ratePlanId) {
this.ratePlanId = ratePlanId;
}
public Integer getPeriod() {
return period;
}
public void setPeriod(Integer period) {
this.period = period;
}
}
And this converter for insert in database:
public class JpaPayloadConverter implements AttributeConverter<Payload, String> {
// ObjectMapper is thread safe
private final static ObjectMapper objectMapper = new ObjectMapper();
private Logger log = LoggerFactory.getLogger(getClass());
#Override
public String convertToDatabaseColumn(Payload attribute) {
String jsonString = "";
try {
log.debug("Start convertToDatabaseColumn");
// convert list of POJO to json
jsonString = objectMapper.writeValueAsString(attribute);
log.debug("convertToDatabaseColumn" + jsonString);
} catch (JsonProcessingException ex) {
log.error(ex.getMessage());
}
return jsonString;
}
#Override
public Payload convertToEntityAttribute(String dbData) {
Payload payload = new Payload();
try {
log.debug("Start convertToEntityAttribute");
// convert json to list of POJO
payload = objectMapper.readValue(dbData, Payload.class);
log.debug("JsonDocumentsConverter.convertToDatabaseColumn" + payload);
} catch (IOException ex) {
log.error(ex.getMessage());
}
return payload;
}
}
I just came across this problem working with the JSONB column type - the binary version of the JSON type, which doesn't map to TEXT.
For future reference, you can define a custom type in H2 using CREATE DOMAIN, as follows:
CREATE domain IF NOT EXISTS jsonb AS other;
This seemed to work for me, and allowed me to successfully test my code against the entity.
Source: https://objectpartners.com/2015/05/26/grails-postgresql-9-4-and-jsonb/
Champagne time! 🍾
Starting with the version 2.11, the Hibernate Types project now provides a generic JsonType that works auto-magically with:
Oracle,
SQL Server,
PostgreSQL,
MySQL, and
H2.
Oracle
#Entity(name = "Book")
#Table(name = "book")
#TypeDef(name = "json", typeClass = JsonType.class)
public class Book {
#Id
#GeneratedValue
private Long id;
#NaturalId
#Column(length = 15)
private String isbn;
#Type(type = "json")
#Column(columnDefinition = "VARCHAR2(1000) CONSTRAINT IS_VALID_JSON CHECK (properties IS JSON)")
private Map<String, String> properties = new HashMap<>();
}
SQL Server
#Entity(name = "Book")
#Table(name = "book")
#TypeDef(name = "json", typeClass = JsonType.class)
public class Book {
#Id
#GeneratedValue
private Long id;
#NaturalId
#Column(length = 15)
private String isbn;
#Type(type = "json")
#Column(columnDefinition = "NVARCHAR(1000) CHECK(ISJSON(properties) = 1)")
private Map<String, String> properties = new HashMap<>();
}
PostgreSQL
#Entity(name = "Book")
#Table(name = "book")
#TypeDef(name = "json", typeClass = JsonType.class)
public class Book {
#Id
#GeneratedValue
private Long id;
#NaturalId
#Column(length = 15)
private String isbn;
#Type(type = "json")
#Column(columnDefinition = "jsonb")
private Map<String, String> properties = new HashMap<>();
}
MySQL
#Entity(name = "Book")
#Table(name = "book")
#TypeDef(name = "json", typeClass = JsonType.class)
public class Book {
#Id
#GeneratedValue
private Long id;
#NaturalId
#Column(length = 15)
private String isbn;
#Type(type = "json")
#Column(columnDefinition = "json")
private Map<String, String> properties = new HashMap<>();
}
H2
#Entity(name = "Book")
#Table(name = "book")
#TypeDef(name = "json", typeClass = JsonType.class)
public class Book {
#Id
#GeneratedValue
private Long id;
#NaturalId
#Column(length = 15)
private String isbn;
#Type(type = "json")
#Column(columnDefinition = "json")
private Map<String, String> properties = new HashMap<>();
}
Works like a charm!
So, no more hacks and workarounds, the JsonType will work no matter what DB you are using.
If you want to see it in action, check out this test folder on GitHub.
A workaround is actually to create a custom column data type in H2 for the jsonb type, and put the query in the datasource url like this:
spring.datasource.url=jdbc:h2:mem:testdb;INIT=create domain if not exists jsonb as text;MODE=PostgreSQL"
Now for tests and integration tests in particular, it would be preferable to use the same DB than your application, via TestContainers
JSON support was added to H2 after the question was asked, with version 1.4.200 (2019-10-14).
However, you rarely need a JSON data type in a database. JSON essentially is just a potentially very long string, so you can use CLOB which is available on most databases.
You do need the JSON data type if you need an SQL function that operates on them, and then only if the database insists that its JSON functions operate on a JSON type instead of on a CLOB. Such functions tend to be database-dependent though.
This is how I solved it in Spring context:
Create /src/test/resources/init.sql
CREATE TYPE "JSONB" AS json;
Configure H2 datasource as follows /src/test/resources/application-test.yml
spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;INIT=RUNSCRIPT FROM 'classpath:init.sql'
username: sa
password: sa
Source article
My problem was with JSONB since H2 does not support it as was already mentioned.
One more problem is that when you insert a json, H2 transforms it into a json object string which makes jackson serialization fail. ex: "{\"key\": 3}" instead of {"key": 3} . One solution is to use FORMAT JSON when inserting the json, but then you need to have duplicate insert files if you are using flyway, for example.
Inspired by the #madz answer I came across with this solution:
Create a custom JsonbType (on production - e.g. main/java/com/app/types/JsonbType.java)
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
public class JsonbType extends JsonBinaryType {
private static final long serialVersionUID = 1L;
}
Create a custom JsonbType (on tests - e.g. test/java/com/app/types/JsonbType.java)
import com.vladmihalcea.hibernate.type.json.JsonStringType;
public class JsonbType extends JsonStringType {
private static final long serialVersionUID = 1L;
#Override
public String getName() {
return "jsonb";
}
}
Create an alias type from JSONB to JSON only on tests (h2):
-- only on H2 database
CREATE TYPE "JSONB" AS TEXT;
note: I'm using flyway which make it easy to do but you can follow #jchrbrt suggestion
Finally you declare the type on your entity model, as follows:
import com.app.types.JsonbType;
#TypeDef(name = "jsonb", typeClass = JsonbType.class)
#Entity(name = "Translation")
#Table(name = "Translation")
#Data
public class Translation {
#Type(type = "jsonb")
#Column(name="translations")
private MySerializableCustomType translations;
}
}
That's it. I hope it helps someone.
In my case we were dealing with PostgreSQL jsonb type in production and H2 for our tests.
I could not test #n00dle 's solution because apparently spring does not support executing a SQL script before Hibernate's ddl-auto=update for our tests so I used another way to solve this.
Here is a gist for it.
The overall idea is to create two package-info files.
One for production and the other for tests and register different types (JsonBinaryType.class for production and TextType.class for tests) to handle them differently for PostgreSQL and H2
I have solved the problem using TEXT type in H2.
One must create a separate database script to create schema in H2 for tests and replace the JSON type by TEXT.
It is still a problem since if you use Json function in queries, you will not be able to test those while with H2.
Example with Kotlin + Spring + Hibernate + Postgres + jsonb column
Create the entity:
#Entity
#TypeDef(name = "jsonb", typeClass = JsonBinaryType::class)
class MyEntity(
#Type(type = "jsonb")
#Column(columnDefinition = "jsonb")
val myConfig: String,
#Id
#GeneratedValue
val id: Long = 0,
)
JsonBinaryType.class comes from https://github.com/vladmihalcea/hibernate-types
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.9.13</version>
</dependency>
Configure your H2 database in spring profile. The key line is this: INIT=create domain if not exists jsonb as other
spring:
profiles: h2
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb;INIT=create domain if not exists jsonb as other;MODE=PostgreSQL;DB_CLOSE_DELAY=-1
username: sa
password: sa
spring.jpa.hibernate.ddl-auto: create
Write the test:
// Postgres test
#SpringBootTest
class ExampleJsonbPostgres(#Autowired private val myEntityRepository: MyEntityRepository) {
#Test
fun `verify we can write and read jsonb`() {
val r = myEntityRepository.save(MyEntity("""{"hello": "world"}"""))
assertThat(myEntityRepository.findById(r.id).get().config).isEqualTo("""{"hello": "world"}""")
}
}
// H2 test
#ActiveProfiles("h2")
#SpringBootTest
class ExampleJsonbH2(#Autowired private val myEntityRepository: MyEntityRepository) {
#Test
fun `verify we can write and read jsonb`() {
val r = myEntityRepository.save(MyEntity("""{"hello": "world"}"""))
assertThat(myEntityRepository.findById(r.id).get().config).isEqualTo("""{"hello": "world"}""")
}
}
Alternatively you can try to define custom type per database in hibernate XML as described here: https://stackoverflow.com/a/59753980/10714479
I am in the same situation as #madz, where we use Postgres in production and H2 for unit tests. In my case i found a bit more simple solution, i think.
We use Liquibase for database migrations, so here i made a conditional migration only to be run on H2, where i change the column type to H2's "other" type.
With the other type, H2 just stores it in the database and doesn't think twice about how the data is formatted etc. This does require however that you are not doing anything with the JSON directly in the database, and only in your application.
My migration looks like this:
# Use other type in H2, as jsonb is not supported
- changeSet:
id: 42
author: Elias Jørgensen
dbms: h2
changes:
- modifyDataType:
tableName: myTableName
columnName: config
newDataType: other
Along with this, i added the following to my test datasource:
INIT=create domain if not exists jsonb as text;
The correct way of avoiding such things is using liquibase or flywaydb to evolve your schema and never ever allow Hibernate to create it.
H2 does not have the JSON data type.
In MySQL the JSON type is just an alias for the LONGTEXT data type so the actual data type for the column will be LONGTEXT.
I'm trying to generate ddl from existing annotated entities using "JPA tools --> Generate Tables from Entities..." in Eclipse Kepler. When I run the task, I get a file with sql scripts to run. The problem is that the order of the columns in the table creation statement fails to comply with the order of the attributes in the class definition.
Example:
#Entity
#Table(name = "news", catalog = "myDatabase")
public class News implements java.io.Serializable {
private long id;
private String newsTitle;
private String newsTitle2;
private String newsText;
private Date created;
#Id
#GeneratedValue
#Column(name = "news_id", unique = true, nullable = false)
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
#Column(name = "news_title")
public String getNewsTitle() {
return this.newsTitle;
}
public void setNewsTitle(String newsTitle) {
this.newsTitle = newsTitle;
}
#Column(name = "news_title2")
public String getNewsTitle2() {
return this.newsTitle2;
}
public void setNewsTitle2(String newsTitle2) {
this.newsTitle2 = newsTitle2;
}
#Lob
#Column(name = "news_text")
public String getNewsText() {
return this.newsText;
}
public void setNewsText(String newsText) {
this.newsText = newsText;
}
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "created")
public Date getCreated() {
return this.created;
}
public void setCreated(Date created) {
this.created = created;
}
}
Script:
CREATE TABLE myDatabase.news (
news_id BIGINT NOT NULL UNIQUE, created DATETIME, news_text LONGTEXT,
news_title VARCHAR(255), news_title2 VARCHAR(255), PRIMARY KEY (news_id))
How can I get the scripts with the order of the columns aligned with the java class?
Thank you very much
Stefano
This is not possible due to the abstraction approach in JPA. Theoretically you don't know the kind of database below nor whether the order is important there.
I'm new to hibernate. My problem is that I have an Oracle database. I have a view in the database. Now I want to use hibernate to retrieve data in that view. Is there any possible solutions?
Below Snippet can solve your problem, which has been extracted from the tutorial: Mapping Hibernate Entities to Views
Database Query
CREATE OR REPLACE VIEW cameron AS
SELECT last_name AS surname
FROM author
WHERE first_name = 'Cameron';
view entity
#Entity
#NamedNativeQuery(name = "findUniqueCameronsInOrder", query = "select * from cameron order by surname", resultClass = Cameron.class)
public class Cameron implements java.io.Serializable {
private static final long serialVersionUID = 8765016103450361311L;
private String surname;
#Id
#Column(name = "SURNAME", nullable = false, length = 50)
public String getSurname() {
return surname;
}
public void setSurname(final String surname) {
this.surname = surname;
}
}
Hibernate mapping file.
<mapping class="examples.hibernate.spring.query.domain.Cameron" />
finally some test !...
#Test
public void findTheCameronsInTheView() throws Exception {
final List<Cameron> camerons = findUniqueCameronsInOrder();
assertEquals(2, camerons.size());
final Cameron judd = camerons.get(0);
final Cameron mcKenzie = camerons.get(1);
assertEquals("Judd", judd.getSurname());
assertEquals("McKenzie", mcKenzie.getSurname());
}
A view is from accessing data nothing different from table, a problem arises when you want to add,update or delete from view.
Please read http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/querysql.html
It' very similar to mapping ordinary database table.
Create an Entity and use your view name as Table name.
#Entity
#Table(name = "rc_latest_offer_details_view")
public class OfferLatestDetailsViewEntity {
#Id
#Column(name = "FK_OFFER_ID")
private int offerId;
#Column(name = "MAX_CHANGED_DTM")
#Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime changedDateTime;
private BigDecimal price;
...
}
Then query for entities same way as you do for normal table.
Working in Hibernate 4, Spring 4.
we can achieve this by using # Immutable annotation in entity class to map database view with Hibernate
For example : I have created one database view user_data which have 2 columns (id and name) and mapped user_data view in the same way as database tables.
#Entity
#Table(name = "user_data")
#Immutable
public class UserView {
#Id
#Column(name = "ID")
private int ID ;
#Column(name = "NAME")
private String name ;
}