Create a Table/Query with JPA/Hibernate in Play Framework - java

So I have Play Framework running at the moment with JPA and Hibernate. I'm completely new to both and the tutorials I've found around the web are above my head.
How in the world can I send a simple query or create a table? This is example code I've written up and I get: "RuntimeException: No EntityManager bound to this thread. Try wrapping this call in JPA.withTransaction, or ensure that the HTTP context is setup on this thread."
package controllers;
import play.Logger;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import play.db.jpa.JPA;
import play.mvc.Controller;
import play.db.*;
public class Database {
public static void initbuild() {
Logger.info("Checking database structure. The database will be restructured if not in the correct format.");
JPA.em().createQuery("create table test");
}
}

Le'ts start saying that you don't want to build the tables by yourself, just write your models under the models package annotating them by #Entity and the JPA plugin will automagically generate the tables matching the models that you have defined.
As for the error the error is raised cause you should annotate the method with the #Transactional annotation.
As stated in the official doc http://www.playframework.com/documentation/2.3.x/JavaJPA
"Every JPA call must be done in a transaction so, to enable JPA for a particular action, annotate it with #play.db.jpa.Transactional. This will compose your action method with a JPA Action that manages the transaction for you"
Hope it helped btw reading the doc and have a look to the computer-jpa example is suggested

Related

JOOQ is not generating classes

I have a problem with JOOQ framework (3.13.4) along with Spring Boot and Java 8.
The problem is that I'm trying to generate domain classes using java code way (instead of using codegen plugin with maven which had some troubles with custom naming strategy provider). So as first let me show You the #Configuration class which contains (at least I believe that it contains) all of the necessary beans:
import com.ormtester.common.base.Measurer;
import com.ormtester.common.utils.enums.OrmType;
import com.ormtester.datasources.config.RouteableDataSource;
import org.jooq.SQLDialect;
import org.jooq.codegen.GenerationTool;
import org.jooq.impl.DataSourceConnectionProvider;
import org.jooq.impl.DefaultConfiguration;
import org.jooq.impl.DefaultDSLContext;
import org.jooq.impl.DefaultExecuteListenerProvider;
import org.jooq.util.xml.jaxb.Schema;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.jooq.meta.jaxb.*;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.annotation.PostConstruct;
import java.util.Properties;
#Configuration
#EnableTransactionManagement
public class JooqConfigurator {
private Properties moduleProperties;
private RouteableDataSource routeableDataSource;
public JooqConfigurator(RouteableDataSource routeableDataSource) {
this.routeableDataSource = routeableDataSource;
try {
moduleProperties = new Properties();
moduleProperties.load(JooqConfigurator.class.getClassLoader()
.getResourceAsStream("jooq.properties"));
} catch (Exception ignore) {}
}
#Bean
public DataSourceConnectionProvider connectionProvider() {
return new DataSourceConnectionProvider(routeableDataSource);
}
#Bean
public ExceptionTranslator exceptionTransformer() {
return new ExceptionTranslator();
}
#Bean
public DefaultConfiguration configuration() {
DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
jooqConfiguration.set(connectionProvider());
jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));
jooqConfiguration.set(SQLDialect.DEFAULT);
return jooqConfiguration;
}
#Bean
public DefaultDSLContext dsl() {
return new DefaultDSLContext(configuration());
}
#PostConstruct
public void generateCode() {
try {
GenerationTool.generate(new org.jooq.meta.jaxb.Configuration()
.withJdbc(new Jdbc()
.withDriver("com.mysql.cj.jdbc.Driver")
.withUrl("jdbc:mysql://localhost:3306/ormtester?useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC")
.withUser("root")
.withPassword("root123"))
.withGenerator(new Generator()
.withName("org.jooq.codegen.JavaGenerator")
.withStrategy(new CustomStrategyProvider())
.withDatabase(new Database()
.withName("org.jooq.meta.mysql.MySQLDatabase")
.withIncludes(".*")
.withExcludes("")
.withSchemata(new SchemaMappingType().withInputSchema("ormtester").withOutputSchema("ormtester"))
.withInputCatalog("ormtester")
.withOutputCatalog("ormtester"))
.withTarget(new Target()
.withPackageName("com.ormtester.jooq.domain")
.withDirectory("jooq/src/main/java"))));
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
}
}
}
RouteableDataSource is a type that extends AbstractRoutingDataSource because in this case I need to have a possibility to change datasource at runtime. This thing is working well in the other regions of the project (or in another words with tools like Hibernate or MyBatis).
As You can see there is a #PostConstruct method which is used for generating domain classes and the problem is that this method doesn't generate any error or something but the classes are also not generated. I've tried to run it using PostgreSQL and Oracle database (of course changing the driver, database name etc.) and the situation is looking exactly the same.
One interesting thing is that when I'm running this code and package com.ormtester.jooq.domain is present - during the method execution domain package is getting removed.
I'd also like to mention that JOOQ autoconfiguration is disabled by excluding JooqAutoConfiguration class through the #SpringBootApplication annotation located at the project's main (starter) class.
IDE is running in administrator's mode and - what can be also interesting - if I will set the breakpoint in the getJavaClassName() method in my custom naming strategy provided (CustomStrategyProvider which extends DefaultGeneratorStrategy class, the breakpoint is reached everytime this method is used.
So does anyone faced the same problem and/or simply can tell me if I'm doing something wrong or something is missing in the code snippet that I've provieded here? I have this problem since about 4 days and now I'm running out of the ideas what can be wrong. I went through the tons of topics on many forums and nothing helped me, including the tutorials on the author's page (which in my opinion simply lacks of important informations).
I'll be really grateful for every help - thanks in advance!
Code generation is a build task, not a runtime task. I can't think of a reasonable scenario where generating code only at runtime would make sense.
The problem is that I'm trying to generate domain classes using java code way (instead of using codegen plugin with maven which had some troubles with custom naming strategy provider)
You have to create a separate maven module (or project) where you build the custom naming strategy, and then add that as a dependency to the jOOQ code generation plugin. This works the same way as with the JPADatabase, where entities have to be placed in a separate maven module.

Purpose of Service Interface Class in Spring Boot

My question is regarding the use of the interface class. I am fairly new to Spring so please bear with me if this is overly simple.
First of all, what is the point of having an IBoxService interface here when you could just declare the find all in BoxService. Secondly, in the controller how is IBoxService being used. Meaning, we are calling IBoxService.findAll(). But, how is this being tied to the BoxService class. What if multiple service classes implemented IBoxService? Is this a java thing or a Spring injection thing. Thanks.
package com.xyz.service;
import com.xyz.model.Box;
import java.util.Set;
public interface IBoxService {
Set<Box> findAll();
}
package com.xyz.service;
import com.xyz.model.Box;
import com.xyz.repository.BoxRepository;
import java.util.Set;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
#Service
#AllArgsConstructor
#Slf4j
#Transactional
public class BoxService implements IBoxService {
#Autowired
private BoxRepository boxRepo;
#Override
public Set<City> findAll() {
return (Set<City>) repository.findAll();
}
}
package com.xyz.controller;
import com.xyz.model.Box;
import com.xyz.service.IBoxService;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
#RestController
#RequestMapping("/api/box")
public class BoxController {
#Autowired
private IBoxService boxService;
#GetMapping
public ResponseEntity<Set<Boxes>> allBoxes() {
return (Set<Box>) boxService.findAll();
}
}
There are various reasons why Service layer interfaces are created. The first and most important reason is testability. You can create mocks of service interface and test your code easily, if you cannot create mocks using a mocking library then you can create test stubs.
One more reason is, we can achieve loose coupling between Controller and Service layer. Suppose you want to entirely change the implementation of service, you can create new service implementation and inject that implementation by injecting new bean by qualifier name
Please understand basic Java and use of interface . Spring boot is just abstraction over Java hence all the basic concepts applies as it is.
Coming back to your questions IBoxService is a interface which allows to inject required implementation of it at controller level. As of now only implementation of IBoxServic is BoxService hence it is getting injected automatically. In case you have multiple implementations you need to use qualifier annotation to specify kind of implementation you need to inject. Or you can create object bu yourself using class names
Consider below:
IBoxService is implemented by two classes BoxService and TiffinBoxService
Now in controller you can inject implementation which you want. Which allow us to achieve principle of interface which is hide internal details.
User which is controller in this case doesn't need to know which class is being use internally as we are using reference of interface.
List interface is best example which has ArrayList and LinkedList as implementation classes.
Hope it is useful !!

MyBatis 3.1.1, how to execute custom SQL code directly

I use PostrgeSQL 9.6 and MyBatis 3.1 (under there is also Java 8 and Spring), and I need to execute a custom query, which I create dynamically with a StringBuilder because of it's complexity.
How can I pass it to my class mapper.xml file ?
I've heard about #SelectProvider, but ? can't find complete examples... Can someone give me a step by step guide ?
I was also reading of The SQL Builder Class, of MyBatis, following the official guide but I miss how to launch that query/object I've created. By the way this doesn't seems the right way to me, because of the complexity of the query I have to build. Following the guide it seems I can't use contidional operators like IF or FOR to create the query string... So it can't work for my use.
thanks.
I only use #SelectProvider for java annotation ,this is a easy way to use it ,
this is my simple example earlier this year
1. create your provider class
package com.mybatis;
import java.util.Map;
import java.util.logging.Logger;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
public class SqlProvider {
public String sqlProvider(Map<String, Object> map){
Logger.getLogger(SqlProvider.class.getName()).info("select * from student where id="+map.get("0"));
return "select * from student where id="+map.get("0");
}
public String findById(#Param("id") int id){
return new SQL(){{
SELECT("id,name,info");
FROM("student");
WHERE("id="+id);
}
}.toString();
}
}
you can use a string produce your sql query ,but also new SQL(){{some clauses such as SELECT(string),WHERE(string) etc.
}}.toString();
2. use your provider class in Mapper interface
we can use #SelectProvider(type=Class,method="methodName")
specified class and method
package com.mybatis;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
/*
*
*Stu is an entity map to your table in db
*
*/
#Mapper
public interface StuMapper {
#SelectProvider(type=SqlProvider.class,method="sqlProvider")
public Stus sqlProvider(Map<String, Object> map);
#SelectProvider(type=SqlProvider.class,method="findById")
public Stus findById_(#Param("id") int id);
}
Last , using you mapper .
more details ,see https://github.com/v5developer/maven-framework-project/blob/master/reference/Java.Persistence.with.MyBatis.3.pdf
especially in chapter 4.
<select id="methodName" resultMap="alarmPieEntry">
${your_raw_sql}
</select>
Just use ${your_raw_sql} variables in myClass_mapper.xml file , don't use #{your_raw_sql} .

How to access AEM Core models in a Sling model

Specifically, I am trying to enable .SVG files to be usable by the core image component.
Right now I am making a sling model that ideally I would like to access the returned values of the getSrc() and getFileReference() classes in the core AEM Component interface located here.
I am very new to AEM development and Sling models. Am I missing some vital functionality that would let me do this?
Here is my code, which probably isn't at all helpful at this point.
package com.site.core.models;
import com.adobe.cq.wcm.core.components.models.Image;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.*;
import org.apache.sling.models.annotations.injectorspecific.*;
import org.apache.sling.settings.SlingSettingsService;
import javax.jcr.Node;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
#Model(adaptables = SlingHttpServletRequest.class)
public class ImageModel {
private String src = Image.getSrc();
return src;
}
As I mentioned in my comment, the link you are referring to is an interface, the implementation of that interface is here
In order to use your own implementation, you have two options:
In the image component html, change the data-sly-use to refer to your impl: com.site.core.models.ImageModel
Create a separate model that implements the Image interface and give it a high ranking to be picked up instead of the existing impl.
Disclaimer: I have not tested #2 but the documentation suggests that it's possible.

Jena, DBpedia: RDF and model name

I'm using dbpedia in my app, and I'm using Jena for the semantic logic.
In Jena, the classes are:
Model: a set of statements
http://jena.sourceforge.net/javadoc/com/hp/hpl/jena/rdf/model/Model.html
Resource: http://jena.sourceforge.net/javadoc/com/hp/hpl/jena/rdf/model/Resource.html
In dbpedia, the rdf code of a resource is this:
e.g. http://dbpedia.org/resource/Frederick_of_Sweden
becomes http://dbpedia.org/data/Frederick_of_Sweden.rdf
If I call:
Model model = maker.createModel( "http://dbpedia.org/data/Frederick_of_Sweden.rdf")
A model with the name 'http://dbpedia.org/data/Frederick_of_Sweden.rdf' is created.
But I actually need to call it 'http://dbpedia.org/resource/Frederick_of_Sweden', to be consistent with the rdf statements. How do I name a model?
If I want to navigate the graph and reach other nodes, which is the best way to store these statements? Do I need a separate model for each dbpedia resource, or can I merge all the statements in one big model?
Thanks for any hint!
Mulone
I don't think the way you create your models really affect the way you download the data. You can create your model with the URI identifier that you like.
Here I show an example that can give some ideas of how to separate the way you get your triples from DBPedia and the way you store them in your backend system.
From your question I assume you are using a database backend so my code is targeted to that kind of backend in Jena.
import java.sql.SQLException;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.SimpleSelector;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.db.DBConnection;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.rdf.model.ModelFactory;
public class TestJena {
public static void main(String[] args) throws java.lang.ClassNotFoundException, java.sql.SQLException {
Class.forName("com.mysql.jdbc.Driver");
//The database backend initialization.
DBConnection connection = new DBConnection(MY_DB, USER, PASS, "mysql");
ModelMaker dbMaker = ModelFactory.createModelRDBMaker(connection);
//A file manager to get the triples from the DBPedia revolvable URI.
FileManager fManager = FileManager.get();
fManager.addLocatorURL();
Model linkedDataModel =
fManager.loadModel("http://dbpedia.org/data/Frederick_of_Sweden.rdf");
//Now we copy the in-memory model into our DB backend.
//When the model is created you can give it the name that you like.
Model dbModel =
dbMaker.createModel("http://dbpedia.org/resource/Frederick_of_Sweden");
dbModel.add(linkedDataModel);
StmtIterator iter = dbModel.listStatements();
while (iter.hasNext()) {
Statement stmt = iter.nextStatement();
System.out.println(stmt);
}
linkedDataModel.close();
dbModel.close();
connection.close();
}
This example prints ...
[http://dbpedia.org/resource/Frederick_i_of_sweden, http://dbpedia.org/ontology/wikiPageRedirects, http://dbpedia.org/resource/Frederick_of_Sweden]
[http://dbpedia.org/resource/Frederick_I_%28of_Sweden%29, http://dbpedia.org/ontology/wikiPageRedirects, http://dbpedia.org/resource/Frederick_of_Sweden]
[http://dbpedia.org/resource/Frederick_I,_Landgrave_of_Hesse-Kassel, http://dbpedia.org/ontology/wikiPageRedirects, http://dbpedia.org/resource/Frederick_of_Sweden]
[http://dbpedia.org:8890/data/Frederick_of_Sweden.rdf, http://xmlns.com/foaf/0.1/primaryTopic, http://dbpedia.org/resource/Frederick_of_Sweden]
(....)

Categories

Resources