Display Oracle Database content in JavaFX TableView - database

I want to display content of an Oracle Database in my TableView "FilmTable".
The structure of the Oracle Databade is: a table column named "FILMTITEL" in the table "LUKA1". The are 4 Film names like Fast and Furious, ...
Hope you can help me and i hope i understand your Solutions to solve my Problem.
I made this code and i got this Errors:
javafx.fxml.LoadException:
/D:/Users/muellerl/workspace/Table_DB/bin/application/gui.fxml
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at application.Main.start(Main.java:15)
at com.sun.javafx.application.LauncherImpl$8.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$7.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$6.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at controller.table_controller.initialize(table_controller.java:39)
... 20 more
If I remove all Table-Code-Contents the error isn't there.
Here is my code i hope you can help me to Display this things.
Main.java
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource(
"/application/gui.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
table_controller.java:
package controller;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import oracle.jdbc.*;
import model.filmtable_data;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
public class table_controller implements Initializable {
Connection conn;
String output;
int zaehler = 0;
#FXML
private TableView<filmtable_data> FilmTable;
#FXML
private TableColumn<filmtable_data, String> Filmtitel_col;
#FXML
private TableColumn<filmtable_data, Integer> ID_col;
#Override
public void initialize(URL location, ResourceBundle resources) {
// Observable List
final ObservableList<filmtable_data> data = FXCollections.observableArrayList();
ID_col.setCellValueFactory(new PropertyValueFactory<filmtable_data, Integer>("rID"));
Filmtitel_col.setCellValueFactory(new PropertyValueFactory<filmtable_data, String>("rFilmtitel"));
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:lukas/1234#10.140.79.39:1521:OTTO");
Statement statement = conn.createStatement();
ResultSet resultset = statement.executeQuery("SELECT FILMTITEL FROM LUKA1");
while (resultset.next()) {
output = resultset.getString(1);
zaehler++;
filmtable_data entry = new filmtable_data(zaehler, output);
data.add(entry);
System.out.println (resultset.getString(1));
System.out.println(output);
}
FilmTable.setItems(data);
statement.close();
} catch (SQLException e) {
System.out.println("Login fehlgeschlagen.");
e.printStackTrace();
}
}
}
filmtable_data.java:
package model;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
public class filmtable_data {
private final SimpleIntegerProperty rID;
private final SimpleStringProperty rFilmtitel;
public filmtable_data (Integer sID, String sFilmtitel) {
this.rID = new SimpleIntegerProperty(sID);
this.rFilmtitel = new SimpleStringProperty(sFilmtitel);
}
public Integer getRID() {
return rID.get();
}
public void setRID(int set) {
rID.set(set);
}
public String getRFilmtitel() {
return rFilmtitel.get();
}
public void setRFilmtitel(String set) {
rFilmtitel.set(set);
}
}

Related

Selenium-webdriver is giving me blank screen of google chrome and a Java null pointer exception. Can anyone suggest?

TestBase which contains the intialization method which is creating problem :
package TestBase;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
public class testBasesetup {
public static Properties prop;
public static WebDriver driver;
public static testBasesetup testBaseObj;
public static EventFiringWebDriver e_driver;
public testBasesetup() throws Exception
{
try {
prop = new Properties();
File src = new File("C:\\Users\\LENOVO\\eclipse-workspace\\Demon\\src\\main\\java\\Config\\config.properties");
FileInputStream ip = new FileInputStream(src) ;
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void initialization() throws Exception
{
String browsername = prop.getProperty("browser");
if(browsername.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "F:\\Eclipse\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
}
else if(browsername.equals("firefox"))
{
System.setProperty("webdriver.gecko.driver", "F:\\Eclipse\\browser\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
String URLtoTest = prop.getProperty("URL");
Thread.sleep(1000);
}
}
Test Class which I am running and this is creating issue. I guess there is some issue with driver variable but don't know the error:
package PageTest;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import Pages.LoginPage;
import TestBase.testBasesetup;
public class LoginPageTest extends testBasesetup{
LoginPage LoginPageObj;
testBasesetup testBaseObj;
public LoginPageTest() throws Exception
{
super();
}
#BeforeMethod
public void setup() throws Exception
{
initialization();
LoginPageObj = new LoginPage();
Thread.sleep(2000);
}
#Test()
public void LoginCheck() throws Exception
{
LoginPageObj.login("first.customer#kkr.com","Potatok");
}
#AfterMethod
public void tearDown()
{
driver.quit();
}
}
Image of error:
Code
Your static instance of WebDriver remain uninitialized. You need to initialize static instance instead of local driver instance in initialization() method. Like Below:
public class testBasesetup {
public static Properties prop;
public static WebDriver driver;
public static testBasesetup testBaseObj;
public static EventFiringWebDriver e_driver;
public testBasesetup() throws Exception
{
try {
prop = new Properties();
File src = new File("C:\\Users\\LENOVO\\eclipse-workspace\\Demon\\src\\main\\java\\Config\\config.properties");
FileInputStream ip = new FileInputStream(src) ;
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void initialization() throws Exception
{
String browsername = prop.getProperty("browser");
if(browsername.equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "F:\\Eclipse\\chromedriver.exe");
**driver = new ChromeDriver();**
}
else if(browsername.equals("firefox"))
{
System.setProperty("webdriver.gecko.driver", "F:\\Eclipse\\browser\\geckodriver.exe");
**driver = new FirefoxDriver();**
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
String URLtoTest = prop.getProperty("URL");
Thread.sleep(1000);
}
}

Null pointer Exception with Page Factory

I was doing some coding to learn PageFactory, but I am getting this error when trying to call a method in another class using page factory
Below is my Elements class:
package pulse.pom.tpr;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
public class PulseElements {
WebDriver driver;
public Actions actions;
// Login Page Elements
#FindBy(how = How.NAME, using = "userId")
WebElement userid;
#FindBy(name = "password")
WebElement password;
#FindBy(name = "dcNumber")
WebElement dcnbr;
#FindBy(css = "body > ion-app > ng-component > ion-nav > page-login > ion-content > div.scroll-content > ion-card > ion-grid > form > ion-list > div.login-button > button > span")
WebElement login;
// TPR button Element
#FindBy(id = "tab-t0-2")
WebElement tpr;
// Send Associate button
#FindBy(xpath = "//*[#id='tabpanel-t0-2']/tpr-summary-page/ion-header[2]/ion-grid/ion-row/ion-col[3]/ion-row/button[1]")
WebElement sendasc;
// Next Button after selecting associate
#FindBy(xpath = "//*[#id='footers']/ion-toolbar/div[2]/ion-row/ion-col[2]/button")
WebElement next1;
// Next Button after selecting Area
#FindBy(xpath = "//*[#id='tabpanel-t0-2']/tpr-send-associates-page/div/ion-footer/button")
WebElement next2;
// Clockin Button
#FindBy(xpath = "//*[#id='tabpanel-t0-2']/tpr-send-associates-page/ion-content/div[2]/div/div/ion-row[2]/ion-col/div[1]")
WebElement clockin;
public PulseElements(WebDriver driver) {
this.driver = driver;
actions = new Actions(driver);
}
#Test(priority = 1)
public void pulseLogin(String uid, String pwd, String dc) {
actions.moveToElement(userid).click().sendKeys(uid);
actions.build().perform();
actions.pause(java.time.Duration.ofSeconds(1));
actions.moveToElement(password).click().sendKeys(pwd);
actions.build().perform();
actions.pause(java.time.Duration.ofSeconds(1));
actions.moveToElement(dcnbr).click().sendKeys(dc);
actions.build().perform();
actions.pause(java.time.Duration.ofSeconds(1));
actions.moveToElement(login).click();
actions.build().perform();
}
#Test(priority = 2)
public void tprClick() {
actions.moveToElement(tpr).click();
actions.build().perform();
}
#Test(priority = 3)
public void sendAssociateButton() {
actions.moveToElement(sendasc).click();
actions.build().perform();
}
public void selectNext1() {
actions.moveToElement(next1).click();
actions.build().perform();
}
public void selectNext2() {
actions.moveToElement(next2).click();
actions.build().perform();
}
public void selectClockin() {
actions.moveToElement(clockin).click();
actions.build().perform();
}
}
And below is my 1st test class for my login page:
package pulse.pom.tpr;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
public class PulseLogin {
public WebDriver driver;
//public PulseElements locateElements=PageFactory.initElements(driver, PulseElements.class);
#Test(priority=1)
public void pulseLoginPage() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver",
"C:\\MyChromeDriver\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();
driver.get("https://mysite/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
PulseElements locateElements=PageFactory.initElements(driver, PulseElements.class);
locateElements.pulseLogin("sysadmin", "1234#", "7036");
locateElements.tprClick();
locateElements.sendAssociateButton();
}
#Test(priority=2)
public void selectAssociate() {
System.out.println("Please select any associate");
Scanner asc = new Scanner(System.in);
asc.close();
}
#Test(priority=3)
public void selectNextButton1(){
PulseElements locateElements=PageFactory.initElements(driver, PulseElements.class);
locateElements.selectNext1();
}
#Test(priority = 4)
public void selectArea() {
System.out.println("Please select area");
Scanner area = new Scanner(System.in);
area.close();
}
#Test(priority=5)
public void selectNextButton2()
{
PulseElements locateElements=PageFactory.initElements(driver, PulseElements.class);
locateElements.selectNext2();
}
}
And my 2nd test class:
package pulse.pom.tpr;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
public class ClockinToClockout {
public WebDriver driver;
public PulseLogin login=PageFactory.initElements(driver,PulseLogin.class);
#Test(priority=1)
public void launchBrowser() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\MyChromeDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
login.pulseLoginPage();
login.selectAssociate();
login.selectNextButton1();
login.selectArea();
login.selectNextButton2();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test(priority=2)
public void makeMove() {
PulseElements ele=PageFactory.initElements(driver, PulseElements.class);
ele.selectClockin();
}
}
Everything else is working fine, but the makeMove() function in my 2nd test class is giving a null pointer exception:
PASSED: launchBrowser
FAILED: makeMove
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:51)
at com.sun.proxy.$Proxy7.getCoordinates(Unknown Source)
at org.openqa.selenium.interactions.internal.MouseAction.getActionLocation(MouseAction.java:65)
at org.openqa.selenium.interactions.MoveMouseAction.perform(MoveMouseAction.java:43)
at org.openqa.selenium.interactions.CompositeAction.perform(CompositeAction.java:36)
at org.openqa.selenium.interactions.Actions$BuiltAction.perform(Actions.java:641)
at pulse.pom.tpr.PulseElements.selectClockin(PulseElements.java:100)
at pulse.pom.tpr.ClockinToClockout.makeMove(ClockinToClockout.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
That's because you need to initialise the PulseElements at the class level and not at the #Test level like you have done it for PulseLogin class.
So, initialise the PulseElements at the class level like:
public class ClockinToClockout {
public WebDriver driver;
public PulseLogin login=PageFactory.initElements(driver,PulseLogin.class);
public PulseElements ele=PageFactory.initElements(driver, PulseElements.class);
#Test(priority=1)
public void launchBrowser() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\MyChromeDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
login.pulseLoginPage();
login.selectAssociate();
login.selectNextButton1();
login.selectArea();
login.selectNextButton2();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test(priority=2)
public void makeMove() {
ele.selectClockin();
}
}

Integrating SQLServer and H2 in Spring Boot Application

I'm trying to integrate SQL Server 2016 and H2 in-memory database for one of my Spring Boot project. All the configuration is set and I'm able to load persistence-unit for both database without any problem. Also, I can query for SQL Server Database and I'm getting my data too. Sql Server JPA configuration is my primary configuration. But, somehow JPA is not able to create entity in H2 database. And after seeing log, I came into the conclusion that the entity that I'm trying to create in H2 is actually trying to find that in SqlServer Datbase. Some part of log for this complain is below:
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'person'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1672)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:460)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:405)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2438)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:208)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:183)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:348)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy152.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 32 common frames omitted
More detail log is posted below. Below are the classes that I've done and configured till now.
application.properties
#Profile Properties
spring.profiles.active=local
# DataSource Properties for SQL Server
app.sqlserver.datasource.url=jdbc:sqlserver://XYZ;database=PQR
app.sqlserver.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
app.sqlserver.jpa.properties.hibernate.ddl-auto=update
app.sqlserver.jpa.properties.hibernate.format_sql=true
app.sqlserver.jpa.properties.hibernate.dialect=<custom_dialect_class>
app.sqlserver.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
app.sqlserver.jpa.show-sql=true
# DataSource Properties for H2
app.h2.datasource.url=jdbc:h2:mem:~/h2/TMV
app.h2.datasource.platform=h2
app.h2.datasource.username=sa
app.h2.datasource.password=
app.h2.datasource.driver-class-name=org.h2.Driver
app.h2.jpa.properties.hibernate.hbm2ddl.auto=create #here I tried using ddl-auto=create
app.h2.jpa.properties.hibernate.format_sql=true
app.h2.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
app.h2.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
app.h2.jpa.show-sql=true
spring.h2.console.enabled=true
spring.h2.console.path=/h2_console
SQLServerDataSourceConfig.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
#Configuration
#EnableJpaRepositories(
entityManagerFactoryRef = "sqlServerEntityManager",
basePackages = "com.xyz.persistence.dao",
transactionManagerRef = "sqlServerTransactionManager"
)
public class SQLServerDataSourceConfig {
#Autowired(required = false)
private PersistenceUnitManager persistenceUnitManager;
#Bean
#ConfigurationProperties("app.sqlserver.jpa")
public JpaProperties sqlServerJpaProperties(){
return new JpaProperties();
}
#Bean
#ConfigurationProperties("app.sqlserver.datasource")
public DataSource sqlServerDataSource(){
return DataSourceBuilder.create().build();
}
#Bean
#Primary
public LocalContainerEntityManagerFactoryBean sqlServerEntityManager(
JpaProperties sqlServerJpaProperties) {
EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(sqlServerJpaProperties);
return builder
.dataSource(sqlServerDataSource())
.packages("com.xyz.persistence.domain")
.persistenceUnit("SQLServer-PU")
.build();
}
#Bean
#Primary
public JpaTransactionManager sqlServerTransactionManager(EntityManagerFactory sqlServerEntityManager) {
return new JpaTransactionManager(sqlServerEntityManager);
}
private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties sqlServerJpaProperties) {
JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(sqlServerJpaProperties);
return new EntityManagerFactoryBuilder(jpaVendorAdapter,
sqlServerJpaProperties.getProperties(), this.persistenceUnitManager);
}
private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(jpaProperties.isShowSql());
adapter.setDatabase(jpaProperties.getDatabase());
adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
return adapter;
}
}
H2DataSourceConfig.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager;
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
#Configuration
#EnableJpaRepositories(
entityManagerFactoryRef = "h2EntityManager",
basePackages = "com.xyz.persistence.daoh2",
transactionManagerRef = "h2TransactionManager"
)
#EntityScan(basePackages = {"com.xyz.persistence.domainh2"})
public class H2DataSourceConfig {
#Autowired(required = false)
private PersistenceUnitManager persistenceUnitManager;
#Bean
#ConfigurationProperties("app.h2.jpa")
public JpaProperties sqlServerJpaProperties() {
return new JpaProperties();
}
#Bean
#ConfigurationProperties("app.h2.datasource")
public DataSource sqlServerDataSource() {
return DataSourceBuilder.create().build();
}
#Bean
public LocalContainerEntityManagerFactoryBean h2EntityManager(
JpaProperties h2JpaProperties) {
EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(h2JpaProperties);
return builder
.dataSource(sqlServerDataSource())
.packages("com.xyz.persistence.domainh2")
.persistenceUnit("H2-PU")
.build();
}
#Bean
public JpaTransactionManager h2TransactionManager(EntityManagerFactory h2EntityManager) {
return new JpaTransactionManager(h2EntityManager);
}
private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties h2JpaProperties) {
JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(h2JpaProperties);
return new EntityManagerFactoryBuilder(jpaVendorAdapter,
h2JpaProperties.getProperties(), this.persistenceUnitManager);
}
private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
adapter.setShowSql(jpaProperties.isShowSql());
adapter.setDatabase(jpaProperties.getDatabase());
adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
return adapter;
}
}
PersonDao.java is the class that is inside “com.xyz.persistence.daoh2”
import com.xyz.persistence.domainh2.Person;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface PersonDao extends CrudRepository<Person, Long> {
}
CustomerDao.java is the class that is inside “com.xyz.persistence.dao”
import com.xyz.persistence.domain.Customer;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface CustomerDao extends CrudRepository<Customer, Long> {
}
Person.class
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
#Table(name = "PERSON")
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private Long id;
#Column(name = "FIRST_NAME", nullable = false)
private String firstName;
#Column(name = "LAST_NAME",nullable = false)
private String lastName;
}
Customer.class
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.io.Serializable;
#Entity
#Data
#AllArgsConstructor
#NoArgsConstructor
#Table(name = "CUSTOMER")
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID")
private Long id;
#Column(name = "FIRST_NAME", nullable = false)
private String firstName;
#Column(name = "LAST_NAME",nullable = false)
private String lastName;
}
This is my H2DataLoader.java class which implements CommandLineRunner to run this class at the time of deployment.
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
#Configuration
#Slf4j
public class H2DataLoader implements CommandLineRunner {
#PersistenceContext(name = "h2EntityManager")
private EntityManager entityManager;
#Override
public void run(String... args) throws Exception {
try {
Query query = entityManager.createNativeQuery("INSERT INTO PERSON (ID, FIRST_NAME, LAST_NAME) VALUES (?, ?, ?)");
query.setParameter(1, 1L);
query.setParameter(2, "Hello");
query.setParameter(3, "World");
query.executeUpdate();
} catch (Exception e) {
log.error("Error Occurred: {}", e);
}
}
}
Spring Boot's main ApiApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
#SpringBootApplication(
scanBasePackages = { "com.xyz.persistence" },
exclude = HibernateJpaAutoConfiguration.class
)
#EnableTransactionManagement
#EnableAsync
#EnableAspectJAutoProxy(proxyTargetClass = true)
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
Finally this is my build.gradle contents:
buildscript {
ext {
springBootVersion = '1.5.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'war'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
configurations {
providedRuntime
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile('org.springframework.boot:spring-boot-starter-aop')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('org.springframework.boot:spring-boot-devtools')
compile('com.microsoft.sqlserver:mssql-jdbc')
compileOnly('org.projectlombok:lombok')
runtime('com.h2database:h2')
}
While running this application it gives following information/errors in console:
.....
[INFO ] 2018-01-18 11:38:41.984 [restartedMain] o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'H2-PU'
[INFO ] 2018-01-18 11:38:44.950 [restartedMain] o.s.o.j.LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'H2-PU'
[INFO ] 2018-01-18 11:38:45.043 [restartedMain] o.s.o.j.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'SQLServer-PU'
[INFO ] 2018-01-18 11:38:46.059 [restartedMain] o.s.o.j.LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'SQLServer-PU'
[INFO ] 2018-01-18 11:38:48.122 [restartedMain] o.s.a.f.CglibAopProxy - Final method [public final org.springframework.http.ResponseEntity org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler.handleException(java.lang.Exception,org.springframework.web.context.request.WebRequest)] cannot get proxied via CGLIB: Calls to this method will NOT be routed to the target instance and might lead to NPEs against uninitialized fields in the proxy instance.
……
Hiding endpoints mapping details, because this is my official project.
……
o.s.b.c.e.t.TomcatEmbeddedServletContainer - Tomcat started on port(s): 8084 (http)
Hibernate:
INSERT
INTO
person
(ID, FIRST_NAME, LAST_NAME)
VALUES
(?, ?, ?)
[WARN ] 2018-01-18 11:38:54.600 [restartedMain] o.h.e.j.s.SqlExceptionHelper - SQL Error: 208, SQLState: S0002
[ERROR] 2018-01-18 11:38:54.600 [restartedMain] o.h.e.j.s.SqlExceptionHelper - Invalid object name 'person'.
[ERROR] 2018-01-18 11:38:54.615 [restartedMain] c.h.m.p.c.H2DataLoader - Error Occurred: {}
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1700)
at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:70)
at com.hms.matching.persistence.config.H2DataLoader.run(H2DataLoader.java:34)
at com.hms.matching.persistence.config.H2DataLoader$$FastClassBySpringCGLIB$$28072180.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at com.hms.matching.persistence.config.H2DataLoader$$EnhancerBySpringCGLIB$$d87b2678.run(<generated>)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
at com…...api.ApiApplication.main(ApiApplication.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
at org.hibernate.engine.query.spi.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:194)
at org.hibernate.internal.SessionImpl.executeNativeUpdate(SessionImpl.java:1373)
at org.hibernate.internal.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:373)
at org.hibernate.jpa.internal.QueryImpl.internalExecuteUpdate(QueryImpl.java:405)
at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:61)
... 27 common frames omitted
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name 'person'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1672)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:460)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:405)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2438)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:208)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:183)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:348)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
at com.sun.proxy.$Proxy152.executeUpdate(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 32 common frames omitted
[INFO ] 2018-01-18 11:38:54.631 [restartedMain] o.s.b.a.l.AutoConfigurationReportLoggingInitializer -
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
[ERROR] 2018-01-18 11:38:54.662 [restartedMain] o.s.b.SpringApplication - Application startup failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:735)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:716)
at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:703)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:304)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
at com……….api.ApiApplication.main(ApiApplication.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:526)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:504)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at com.hms.matching.persistence.config.H2DataLoader$$EnhancerBySpringCGLIB$$d87b2678.run(<generated>)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:732)
... 11 common frames omitted
Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:58)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517)
... 22 common frames omitted
[INFO ] 2018-01-18 11:38:54.662 [restartedMain] o.s.b.c.e.AnnotationConfigEmbeddedWebApplicationContext - Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#7ab1fbb9: startup date [Thu Jan 18 11:38:31 CST 2018]; root of context hierarchy
I don’t know why it is trying to look for Person object in SQL Server. Do anyone have some idea on this. I'm looking after this issue for last 24 hrs. But, didn't find anything. Person table should be created in H2 database. Everything is fine SQL Server side.
I don't know if this will solve your problem but :
This is not the way JPA is supposed to work. Instead create a new Person() object, which should be an #Entity, and set its attributes, then entityManager.persist(person).

Junit Testing Framework for google app engine

// My test class
I want to do junit test of the below code but it is not performing and throwing some errors. I have imported all the jar files but still the junit testing is not doing its task.
package com.full.notetakingapplicationtest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import static org.mockito.Mockito.atLeastOnce;
import com.full.notetakingapplication.RegistrationClass;
import com.full.notetakingapplication.UserDetailsClass;
import static org.mockito.Mockito.verify;
import com.google.appengine.tools.development.testing.LocalAppIdentityServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig;
import junit.framework.TestCase;
public class UserDetailsClassTest extends TestCase {
public final LocalServiceTestHelper helper = new LocalServiceTestHelper(
new LocalDatastoreServiceTestConfig(),
new LocalTaskQueueTestConfig().setDisableAutoTaskExecution(true),
new LocalTaskQueueTestConfig().setShouldCopyApiProxyEnvironment(true),
new LocalAppIdentityServiceTestConfig());
#Before
public void setUp(){
helper.setUp();
}
#After
public void tearDown(){
helper.tearDown();
}
#InjectMocks
UserDetailsClass user = Mockito.mock(UserDetailsClass.class);
#InjectMocks
RegistrationClass register = new RegistrationClass();
#SuppressWarnings("static-access")
#Test
public void testRegisterUser_usingMockito(){
String userName = "Pravin";
String userEmailId = "pravin.bala#adaptavantcloud.com";
String userPassword = "success";
register.setUserName(userName);
register.setUserEmailId(userEmailId);
register.setUserPassword(userPassword);
Assert.assertTrue(user.registerUser(register));
verify(user, atLeastOnce()).registerUser(register);
}
}
// My Error on Testing the method.
UserDetailsClassTest.testRegisterUser_usingMockito
testRegisterUser_usingMockito(com.full.notetakingapplicationtest.UserDetailsClassTest)
java.lang.NoSuchMethodError: com.google.appengine.repackaged.com.google.io.protocol.ProtocolSupport.freezeString(Ljava/lang/Object;)Ljava/lang/Object;
at com.google.storage.onestore.v3.OnestoreEntity$Reference.freeze(OnestoreEntity.java:7656)
at com.google.storage.onestore.v3.OnestoreEntity$Reference$1.<init>(OnestoreEntity.java:7555)
at com.google.storage.onestore.v3.OnestoreEntity$Reference.<clinit>(OnestoreEntity.java:7552)
at com.google.appengine.api.datastore.KeyTranslator.convertToPb(KeyTranslator.java:48)
at com.google.appengine.api.datastore.EntityTranslator.convertToPb(EntityTranslator.java:51)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$4.toPb(AsyncDatastoreServiceImpl.java:178)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$4.toPb(AsyncDatastoreServiceImpl.java:155)
at com.google.appengine.api.datastore.Batcher$BatchIterator.<init>(Batcher.java:180)
at com.google.appengine.api.datastore.Batcher$2.<init>(Batcher.java:317)
at com.google.appengine.api.datastore.Batcher.getBatches(Batcher.java:317)
at com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.doBatchPut(AsyncDatastoreServiceImpl.java:365)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:293)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl$4.runInternal(BaseAsyncDatastoreServiceImpl.java:261)
at com.google.appengine.api.datastore.TransactionRunner.runWriteInTransaction(TransactionRunner.java:53)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:263)
at com.google.appengine.api.datastore.BaseAsyncDatastoreServiceImpl.put(BaseAsyncDatastoreServiceImpl.java:234)
at com.google.appengine.api.datastore.DatastoreServiceImpl.put(DatastoreServiceImpl.java:56)
at com.full.notetakingapplication.UserDetailsClass.registerUser(UserDetailsClass.java:25)
at com.full.notetakingapplicationtest.UserDetailsClassTest.testRegisterUser_usingMockito(UserDetailsClassTest.java:60)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Error while connecting to MySql database using mapreduce job

I was trying the following code for connecting to a mysql database using a map-reduce job. I am facing the following error which is posted below. I have placed checkpoints in my code
which indicate that the part of the job till the job is actually run is run correctly, afterwards the job fails...
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.SequenceFileOutputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.apache.hadoop.mapred.lib.db.DBConfiguration;
//import org.apache.hadoop.mapred.lib.db.DBInputFormat;
import org.apache.hadoop.mapred.lib.db.DBInputFormat;
import org.apache.hadoop.mapred.lib.db.DBOutputFormat;
import org.apache.hadoop.mapred.lib.db.DBWritable;
public class TweetWordCount {
public static class TweetWordCountMapper extends MapReduceBase implements
Mapper<LongWritable, GetTweets, Text, IntWritable> {
private final static IntWritable intTwordsCount = new IntWritable(1);
private Text strTwoken = new Text();
public void map(LongWritable key, GetTweets value,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
System.out.println("checkpoint4");
GetTweets tweets = new GetTweets();
tweets.strTweet = value.strTweet;
//TwitterTokenizer twokenizer = new TwitterTokenizer();
//List<String> twokens = twokenizer.twokenize(value.strTweet.toString());
output.collect(new Text(value.strTweet.toString()), intTwordsCount);
System.out.println("checkpoint5");
}
}
public static class TweetWordCountReducer extends MapReduceBase implements
Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
System.out.println("checkpoint6");
int intTwokenCount = 0;
while (values.hasNext()) {
intTwokenCount += values.next().get();
}
output.collect(key, new IntWritable(intTwokenCount));
System.out.println("checkpoint6");
}
}
public static void main(String[] args) throws Exception {
System.out.println("checkpoint1");
JobConf twokenJobConf = new JobConf(new Configuration(),TweetWordCount.class);
//JobConf twokenJobConf = new JobConf(TweetWordCount.class);
twokenJobConf.setJobName("twoken_count");
twokenJobConf.setInputFormat(DBInputFormat.class); //Set input format here
twokenJobConf.setOutputFormat(TextOutputFormat.class);// Sets the output format
Object out = new Path("twokens");
twokenJobConf.setMapperClass(TweetWordCountMapper.class);
twokenJobConf.setCombinerClass(TweetWordCountReducer.class);
twokenJobConf.setReducerClass(TweetWordCountReducer.class);
twokenJobConf.setOutputKeyClass(Text.class);
twokenJobConf.setOutputValueClass(IntWritable.class);
DBConfiguration.configureDB(twokenJobConf, "com.mysql.jdbc.Driver",
"jdbc:mysql://localhost/test", "root", "root"); //Specifies the DB configuration
String[] fields = {"Tweet"}; //Specifies the Fields to be fetched from DB
DBInputFormat.setInput(twokenJobConf, GetTweets.class, "NewGamil",
null /* conditions */, "Tweet", fields); // Specifies the DB table and fields
//SequenceFileOutputFormat.setOutputPath(twokenJobConf, (Path) out);
FileOutputFormat.setOutputPath(twokenJobConf, (Path) out);
System.out.println("checkpoint2");
JobClient.runJob(twokenJobConf);
System.out.println("checkpoint3");
}
public static class GetTweets implements Writable, DBWritable {
String strTweet;
public GetTweets() {
}
public void readFields(DataInput in) throws IOException {
System.out.println("checkpoint 2a");
this.strTweet = Text.readString(in);
}
public void readFields(ResultSet resultSet) throws SQLException {
System.out.println("checkpoint 3a");
// this.id = resultSet.getLong(1);
this.strTweet = resultSet.getString(1);
}
public void write(DataOutput out) throws IOException {
}
public void write(PreparedStatement stmt) throws SQLException {
}
}
}
rv#ramanujan:~$ hadoop jar Twit.jar
Warning: $HADOOP_HOME is deprecated.
checkpoint1
checkpoint2
13/03/22 17:16:12 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
13/03/22 17:16:12 INFO mapred.JobClient: Cleaning up the staging area hdfs://localhost:54310/home/rv/hadoopfiles/mapred/staging/rv/.staging/job_201303221600_0008
Exception in thread "main" java.lang.RuntimeException: Error in configuring object
at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:93)
at org.apache.hadoop.util.ReflectionUtils.setConf(ReflectionUtils.java:64)
at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:117)
at org.apache.hadoop.mapred.JobConf.getInputFormat(JobConf.java:575)
at org.apache.hadoop.mapred.JobClient.writeOldSplits(JobClient.java:981)
at org.apache.hadoop.mapred.JobClient.writeSplits(JobClient.java:973)
at org.apache.hadoop.mapred.JobClient.access$600(JobClient.java:172)
at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:889)
at org.apache.hadoop.mapred.JobClient$2.run(JobClient.java:842)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:416)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1059)
at org.apache.hadoop.mapred.JobClient.submitJobInternal(JobClient.java:842)
at org.apache.hadoop.mapred.JobClient.submitJob(JobClient.java:816)
at org.apache.hadoop.mapred.JobClient.runJob(JobClient.java:1253)
at TweetWordCount.main(TweetWordCount.java:107)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.hadoop.util.RunJar.main(RunJar.java:156)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.hadoop.util.ReflectionUtils.setJobConf(ReflectionUtils.java:88)
... 20 more
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.hadoop.mapred.lib.db.DBInputFormat.configure(DBInputFormat.java:271)
... 25 more
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at org.apache.hadoop.mapred.lib.db.DBConfiguration.getConnection(DBConfiguration.java:123)
at org.apache.hadoop.mapred.lib.db.DBInputFormat.configure(DBInputFormat.java:266)
... 25 more
Make sure you use -libjars in commands. like the following :
bin/hadoop jar wordcount.jar org.myorg.DBCountPageView -libjars mysql-connector-java-5.1.26-bin.jar
Please reference http://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html's usage part
Not sure what the scope of your app is (learning, dev, etc.) but I would suggest to use Sqoop to interact with a relational database such as MySQL.

Resources