Spring Data Projection not working - spring

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

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.

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

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.

How to block Cassandra from trying to connect automatically

I have this project where I'm trying to connect to different DB types based on configuration.
I have it working for Mongo and MySQL and switch by loading Beans by using #ConditionalOnProperty(name = "settings.data.source", havingValue = "mongodb")
Now I want to add Cassandra, but once I added the following dependency to my pom, it starts trying to connect to Cassandra nodes on localhost.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
I want to have more control over when the Cassandra resources are loaded.
It doesn't try to connect automatically when I added Mongo dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
Anyone familiar with this behavior? How can I control this? I don't always need a Cassandra connection...
You may disable Cassandra auto configuration,
#SpringBootApplication
#EnableAutoConfiguration(exclude={CassandraDataAutoConfiguration.class})
Yep, That's it, thanks!
#EnableAutoConfiguration(exclude={
CassandraDataAutoConfiguration.class,
MongoDataAutoConfiguration.class,
MongoRepositoriesAutoConfiguration.class,
MongoAutoConfiguration.class})
For both my #SpringBootApplication and #Configuration classes.

WildFly 10, JCache - method caching

i have simple application using Spring Boot. I wanted allow method caching with JSR107 - JCache. So with help of tutorial i put together this code :
#CacheResult(cacheName = "testpoc")
public Country getCountry(Integer id){
System.out.println("---> Loading country with code '" + id + "'");
return new Country(id, "X", "Title");
}
with this POM file
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.cache</groupId>
<artifactId>cache-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
...
(dependency 'spring-boot-starter-web' is there for simple REST service which call getCountry method)
Everything works like documentations says - method is invoked only once.
Now i wanted to try it on WildFly 10 application server
I have modified pom file :
excluded tomcat
exluded spring-boot-starter-cache
added infinispan-jcache (because i want to use cache configured / managed by wildfly in standalone/domain.xml)
Check pom file here on pastebin.
Problem is, that i am receiving following error :
Cannot find cache named 'java:jboss/infinispan/app-cache'
(i have tried to use both JNDI assigned and name to infinispan cache configured in wildfly).
Following code created Cache object (so i can used it) :
CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
Cache<String, String> cache = cacheManager.createCache("testpoc", new MutableConfiguration<String, String>());
Question :
It is possible to use JCache method caching on WildFly 10 using Infinispan managed by WildFly ?
Or Infinispan should be used for method caching like JCache, hence JCache has "more functionality" than Infinispan.
Thank you very much
PS :It is not problem for me to put whole code on github and post link - it is few lines of code ...
There are a couple of problems with your approach so let me go through them in steps.
At first you need to use proper Infinispan setup. Infinispan bits shipped with WF should be considered as internal or private. In order to use Infinispan in your app properly - either add org.infinispan:infinispan-embedded to your deployment or install Infinispan Wildfly modules. You can find installation guide here (it's a bit outdated but still, the procedure is exactly the same - unpack modules into WF and use Dependencies MANIFEST.MF entry).
Once you have successfully installed Infinispan (or added it to your app), you need to consider whether you want to use Spring Cache or JCache. If you're only interested in using annotations - I would recommend the former since it's much easier to setup (all you need to do is to add #EnableCaching to one of your configurations). Finally with Spring Cache you will create an Infinispan CacheManager bean. An example can be found here.
Final note - if you still need to use JCache - use this manual to setup Caching Provider.

Resources