How use java.time.LocalDate using Hibernate5 and Vaadin8? - maven

I'm new on Vaadin and I'm trying to do a simple crud application.
I was starting from Vaadin CRUD sample; then I modify the backend project to use Hibernate and the ui project to manage the various tables.
I can do that for simple data type like varchar and int, now I would add a date's field but it returns me this error:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: '\xAC\xED\x00\x05sr\x00\x0Djava.time.Ser\x95]\x84\xBA\x1B"H\xB2\x0C\x00\x00xpw\x07\x03\x00\x00\x07\xE1\x06\x1Ax' for column 'data_nascita' at row 1
"DATA_NASCITA" is a date column on my mysql DB and the entity has a LocalDate linked to this field.
I found somewhere that this new java api is compatible only with Hibernate5 so in my pom file I add:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-java8</artifactId>
<version>5.1.0.Final</version>
</dependency>
and into the entity class I replace java.util.Date with java.time.LocalDate and delete the annotation #temporal.
The input class for this field is a DateField of Vaadin framework.
Which could be the problem?
P.S.: I'm blind, so I'm compiling the code with Netbeans8 but I modify the code with notepad++ so, if someone use the same technologies and hasn't any problem please tell me because I can't be sure that the problem is something with build process.

IF your hibernate version is greater than 5.2.+ you can just using column as below:
#Column
private LocalDate date;
Otherwise you need install hibernate-java8 module into your project.
On the other hand, you shouldn't install hibernate-java8 module into your project if the hibernate version >= 5.2, maybe it will makes some conflict, since the hibernate-java8 module is merged into hibernate.

Related

Can vertx and traditional jdbc clients work together in quarkus?

In my quarkus project, I have added the following dependencies to create a non-blocking asynchronous project
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-mutiny-vertx-web-client</artifactId>
</dependency>
But now I want to write data to mysql database, so I want to add the following dependency to the project
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-mysql</artifactId>
</dependency>
But it seems that this database driver and orm are blocking, so what I want to ask is, does vert.x work with traditional jdbc in quarkus?
Another question is, is the quarkus framework an asynchronous, non-blocking framework? What is its relationship to vert.x? When the qurakus framework Added vert.x dependency, did the whole project require non-blocking?
Quarkus can be reactive or non reactive, it depends which extensions you choose to use.
Hibernate ORM Panache is for when you want to access the database in a non reactive way using Hibernate ORM and Panache.
The same is true for JDBC, it's not reactive so you have to use something else if you want your whole app to be reactive.
In this scenario, you probably want to try these two extensions:
quarkus-hibernate-reactive-panache: See the Hibernate Reactive with panache quickstart for an example
quarkus-reactive-mysql-client: For allowing reactive access to the db. See the guide for Vert.x SQL Clients on the quarkus website
On the Quarkus website, there is also an intro on reactive for Quarkus.
Example of how to use the Vert.x SQL client from the guide:
#Path("fruits")
public class FruitResource {
#Inject
io.vertx.mutiny.mysqlclient.MySQLPool client;
}
client.query("DROP TABLE IF EXISTS fruits").execute()
.flatMap(r -> client.query("CREATE TABLE fruits (id SERIAL PRIMARY KEY, name TEXT NOT NULL)").execute())
.flatMap(r -> client.query("INSERT INTO fruits (name) VALUES ('Orange')").execute())
.flatMap(r -> client.query("INSERT INTO fruits (name) VALUES ('Pear')").execute())
.flatMap(r -> client.query("INSERT INTO fruits (name) VALUES ('Apple')").execute())
.await().indefinitely();
Example with Hibernate Reactive and Panache from the quickstart:
#Entity
public class Fruit extends PanacheEntity {
#Column(length = 40, unique = true)
public String name;
public Fruit() {
}
public Fruit(String name) {
this.name = name;
}
}
#GET
public Uni<List<Fruit>> get() {
return Fruit.listAll(Sort.by("name"));
}
A complete list of quickstarts for Quarkus is available on GitHub.
Look for the ones with the word reactive and you will have several examples with different extensions.
To conclude, one approach is to replace the Hibernate ORM and JDBC extensions with the following:
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-reactive-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-mysql-client</artifactId>
</dependency>
Answer to your questions:
is the quarkus framework an asynchronous, non-blocking framework? What is its relationship to vert.x?
Quarkus is designed to work with popular Java standards, frameworks, and libraries. So, it being reactive or not depends from which extensions you pick and how you design your app. Vert.x is one of the tools that are available to you (See What's Quakrkus?)
When the quarkus framework Added vert.x dependency, did the whole project require non-blocking?
No. For example, you can have both the JDBC driver and the Vert.x SQL client extensions on the classpath and get one or the other via injection when you need to use them.

#XmlTransient not working for Json

I am creating a simple API using Jersey Jax-Rs. I am using #XmlTransient to ignore the POJO being displayed in the output for a 1-to-many relationship. It works fine for XML output but the POJO gets displayed while outputting JSON. Do I need to add any dependency?
I am using the following dependency:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
If you are using json-media-json-binding.jar , There is an annotation "#JsonbTransient" which helped me achieve this.

Flyway Invocation of init method failed

I'm trying to set up Flyway to work with Spring JPA. I'm stuck at:
Error creating bean with name 'org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration': Invocation of init method failed; nested exception is java.lang.IllegalArgu: name
Stack trace isn't giving me much more to work with. I have a previous database where many entities have a name column. I'm guessing Flyway is having some trouble extracting the existing database schema? I looked trough Spring, Spring Boot, and Flyway docs, but couldn't find anything for this.
I have a PostgreSQL database. Here are the related dependencies from my pom.xml:
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.2-1002.jdbc4</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>4.0.3</version>
</dependency>
Here is my Flyway configuration in application.properties:
flyway.baseline-version=1
flyway.baseline-on-migrate=true
flyway.check-location=true
flyway.locations=${MIGPATH}
I think locations is correct, because I had a mistake with it earlier and after fixing that, the error message changed. It's a path to a folder with 1 file called V1__f.sql - here are its contents:
ALTER TABLE category ADD hidden boolean NOT NULL;
ALTER TABLE expense ADD details VARCHAR(256);
Since the error was related to name and my migrations don't concern that field, I'm guessing this error is not caused by the contents of the migration file. In any case I believe the contents of the migration file is correct (my previously existing database + those 2 changes should be compatible with the schema implicitly defined by my JPA entity definitions).

Spring Data Projection not working

I want to use spring projection in my project. I am doing exactly as mentioned at this url http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections
interface NoAddresses {
String getFirstName();
String getLastName();
}
The only difference is my interface is public. and is not in the same package as the repository. Is that a requirement?
Still I see the whole entities are being returned, instead of just the projected columns.
I have
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.10.2.RELEASE</version>
</dependency>
Doesn't work. Do i need to add some dependency to make the projection work? I am not using spring-boot but just the spring-data stuff in my project, running on tomcat server.
thanks
Chahat

Jackson 2.4.2 Date serialization not milliseconds

I'm using Spring MVC 3.2.x with Jackson 2.4.2 for JSON web services.
I have objects that contain java.util.Date and the JSON contains a string representation of just the date portion: ("2014-09-15"). this goes against the Jackson documentation that says dates by default get marshalled as milliseconds epoch format (http://wiki.fasterxml.com/JacksonFAQDateHandling).
I would like the date members to be returned as milliseconds format, what am I missing here?
Here is my jackson libraries in my pom file:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.2</version>
</dependency>
I have annotation-driven in my applicationContext:
Thanks!
Alessandro Ferrucci
In my case, problem was in Spring Data REST(2.2.1) disabling WRITE_DATES_AS_TIMESTAMPS by default. I am sure Spring MVC might be doing the same, but I unable to locate that code-commit.
However I was able to locate code-commit in case of Spring Data REST:
DATAREST-336 - Default to ISO8601 date rendering
https://github.com/spring-projects/spring-data-rest/commit/2f1e9824cddb6085c9fd86f1e0b84721497669bb

Resources