It looks like basical, but I am new with Struts2.
I want to send a new product created with name, price and category.
I work with struts2 version 2.5.26, Maven, tomcat 9.0, java jdk 1.8.0_241
It seams that the code I use to send the product is wrong, and Action defined in struts.xml can't get it.
My JSP file to send the product :
<%# page pageEncoding="UTF-8" %>
<%# taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Nouveau produit</title>
</head>
<body>
<h1>Ajouter un nouveau produit</h1><br><br>
<form action="newProduit.action" method="post">
<div >
<s:textfield name="produit.nom" label="Nom " />
</div><br>
<div>
<s:textfield name="produit.prix" class="formH len150" label="Prix " />
</div><br>
<div style="position:relative;">
<label for="cat" class="formH len150">Catégorie :</label>
<select id="cat" name="produit.categorie" class="formH len150">
<option value="">--Choisir--</option>
<s:iterator value="categories" var="c">
<option value=" ${c.id } " > ${c.nom } </option>
</s:iterator>
</select>
</div><br>
<div >
<s:submit class="btn btn-vert" value="Envoyer" />
</div><br>
<div >
<s:a href="listeProduits.action" class="btn" >Retour liste produits</s:a>
</div>
</form>
</body>
</html>
The struts.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="actionsDiverses" extends="struts-default,json-default" namespace="/actions" >
<!-- other actions -- >
<action name="newProduit" method="nvProduit" class="actions.ProduitsAction">
<result name="success" type="redirect">listeProduits</result>
</action>
</package>
</struts>
The ProduitsAction.java file :
package actions;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import entities.Produit;
import services.IMetiersBoutiqueFactory;
import services.MetiersBoutiqueFactory;
public class ProduitsAction extends ActionSupport implements ModelDriven{
private static final long serialVersionUID = 1L;
private IMetiersBoutiqueFactory services = new MetiersBoutiqueFactory();
private Produit produit;
public String nvProduit() {
try {
services.ajouterProduit(produit);
}catch (Exception e) {
e.getStackTrace();
}
return SUCCESS;
}
// other methods, properties, getters and setters
public Produit getProduit() {
return produit;
}
public void setProduit(Produit produit) {
this.produit = produit;
}
#Override
public Produit getModel() {
return produit;
}
}
The error sent is :
com.opensymphony.xwork2.config.ConfigurationException: No result defined for action actions.ProduitsAction and result input
I tried some other solutions but the best I get is a product with nom=null, prix=null, categorie=null.
I guess my problem is in these files.
I may send you the other files or methods
The entity Produit :
package entities;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
#Entity
#Table(name = "produit")
public class Produit {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String nom;
private Double prix;
#ManyToOne
#JoinColumn(name="id_categorie", nullable=true)
private Categorie categorie;
public Produit(Long id, String nom, Double prix, Categorie categorie) {
super();
this.id = id;
this.nom = nom;
this.prix = prix;
this.categorie = categorie;
}
public Produit() {}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public Double getPrix() {
return prix;
}
public void setPrix(Double prix) {
this.prix = prix;
}
public Categorie getCategorie() {
return categorie;
}
public void setCategorie(Categorie categorie) {
this.categorie = categorie;
}
}
I am currently working on the Netbeans IDE with a Hibernate Framework project to retrieve values from the database and display it in a table structure. I have created the mapping with the database tables and the controller method to retrieve the values.
I am quite new to the hibernate framework and am not quite sure how to display the list of roles passed from the RoleController.java in my Role.jsp view. Any suggestions in this regard will be highly appreciated.
RoleController.java
package management;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
/**
*
* #author Taro
*/
#Named(value = "roleController")
#ManagedBean
#SessionScoped
public class RoleController implements Serializable {
int employeeId;
DataModel roleTitles;
String roleTitle;
RoleHelper helper;
public RoleController() {
helper = new RoleHelper();
}
public DataModel getRoleTitles() {
if (roleTitles == null) {
System.out.println("Successful");
roleTitles = new ListDataModel(helper.getRoleTitles());
}
return roleTitles;
}
}
Roles.jsp
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-8">
<div class="card">
<div class="header">
<h4 class="title">Available Roles</h4>
<p class="category">A list of Role Titles</p>
</div>
<div class="content">
<!--Display table of roles with an edit button against each role-->
</div>
</div>
</div>
...
RoleHelper.java
public class RoleHelper {
Session session = null;
public RoleHelper() {
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
}
/*
* Method : getRoleTitles
* #description Retrieve all the unique role titles
*/
public List getRoleTitles() {
List<Role> roleList = null;
try {
org.hibernate.Transaction tx = session.beginTransaction();
Query q = session.createQuery ("select distinct role.title from Role as role");
roleList = (List<Role>) q.list();
} catch (Exception e) {
e.printStackTrace();
}
return roleList;
}
...
Role.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 21, 2017 1:12:44 PM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="management.Role" table="ROLE" schema="APP" optimistic-lock="version">
<id name="roleid" type="int">
<column name="ROLEID" />
<generator class="assigned" />
</id>
<property name="employeeid" type="java.lang.Integer">
<column name="EMPLOYEEID" />
</property>
<property name="title" type="string">
<column name="TITLE" length="40" />
</property>
</class>
</hibernate-mapping>
Role.java
public class Role implements java.io.Serializable {
private int roleid;
private Integer employeeid;
private String title;
public Role() {
}
public Role(int roleid) {
this.roleid = roleid;
}
public Role(int roleid, Integer employeeid, String title) {
this.roleid = roleid;
this.employeeid = employeeid;
this.title = title;
}
public int getRoleid() {
return this.roleid;
}
public void setRoleid(int roleid) {
this.roleid = roleid;
}
public Integer getEmployeeid() {
return this.employeeid;
}
public void setEmployeeid(Integer employeeid) {
this.employeeid = employeeid;
}
public String getTitle() {
return this.title;
}
public void setTitle(String title) {
this.title = title;
}
}
Looks like you are using JSF within your project, so I suggest to use capabilities of this framework for building the UI. Thus you can create a facelet index.xhtml with following code.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>JSF Example</title>
</h:head>
<h:body>
<h:dataTable value="#{roleController.getRoleTitles()}" var="roles">
<h:column>
<f:facet name="header">id</f:facet>
#{roles.roleid}
</h:column>
<h:column>
<f:facet name="header">Title</f:facet>
#{roles.title}
</h:column>
<h:column>
<h:form>
<h:commandButton value="Delete" action="#{roleController.delete(roles.roleid)}"/>
</h:form>
</h:column>
</h:dataTable>
</h:body>
</html>
Result of such code will be following.
I have also reworked a little bit your RoleController.java so it can support delete action:
import com.mberazouski.stackoverflow.components.utils.RoleHelper;
import com.mberazouski.stackoverflow.domain.Role;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import java.io.Serializable;
import java.util.List;
#ManagedBean
#ViewScoped
public class RoleController implements Serializable {
private static final long serialVersionUID = 9160307746480440676L;
private RoleHelper helper;
public RoleController() {
helper = new RoleHelper();
}
public List<Role> getRoleTitles() {
return helper.getRoleTitles();
}
public void delete(int id) {
helper.delete(id);
}
}
Also RoleHelper.java was reworked because now it returns collection of Role objects.
import com.mberazouski.stackoverflow.domain.Role;
import com.mberazouski.stackoverflow.persistence.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.List;
public class RoleHelper {
public List<Role> getRoleTitles() {
List<Role> roleList = null;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
try {
Query q = session.createQuery("FROM Role");
roleList = (List<Role>) q.list();
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}
return roleList;
}
public void delete(int roleid) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction transaction = session.beginTransaction();
try {
Query q = session.createQuery("FROM Role role WHERE role.roleid = " + roleid);
Role role = (Role) q.list().get(0);
session.delete(role);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
}
}
}
In addition I have some clues regarding you Role.hbm.xml where I used increment generator instead assigned, but I'm not sure which DB did you use, so possible increment aren't supported by your DB server.
Hope that this information will be enough to continue with your work.
I am using struts2 and hibernate Integration by following this link http://www.tutorials4u.net/struts2-tutorial/struts2_crud_example.html and trying to update the records but instead of updating it is inserting new records , I have seen all the hibernate update questions before posting this post but none of them worked for me so Please I am requesting to read my question before marking it as duplicate, as I had tried a lot from the already posted questions and answers...
AddStudentAction.java
public class AddStudentAction extends ActionSupport implements
ModelDriven<Student> {
public AddStudentAction() {
// TODO Auto-generated constructor stub
}
Student student = new Student();
private String firstName;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstNameSearch(String firstName) {
this.firstName = firstName;
}
List<Student> students = new ArrayList<Student>();
List<Student> studentFirstNames = new ArrayList<Student>();
public List<Student> getStudentFirstNames() {
return studentFirstNames;
}
public void setStudentFirstNames(List<Student> studentFirstNames) {
this.studentFirstNames = studentFirstNames;
}
StudentDAO dao = new StudentDAO();
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public StudentDAO getDao() {
return dao;
}
public void setDao(StudentDAO dao) {
this.dao = dao;
}
#Override
public Student getModel() {
// TODO Auto-generated method stub
return student;
}
#Override
public String execute() {
// TODO Auto-generated method stub
dao.addStudent(student);
String f = student.getFirstName();
String l = student.getLastName();
int m = student.getMarks();
System.out.println(f + l + m + "Inside execute method");
return "success";
}
public String updateStudent() {
dao.addStudent(student);
return "success";
}
public String listStudents() {
students = dao.getStudents();
return "success";
}
public String editStudent() {
HttpServletRequest request = (HttpServletRequest) ActionContext
.getContext().get(ServletActionContext.HTTP_REQUEST);
student = dao.listStudentById(Integer.parseInt((request
.getParameter("id"))));
student.setId(Integer.parseInt((request.getParameter("id"))));
System.out.println(request.getParameter("id") + "id in editStudent");
dao.updateStudent(student);
return SUCCESS;
}
StudentDAO.java
public class StudentDAO {
#SessionTarget
Session session;
#TransactionTarget
Transaction transaction;
public void addStudent(Student student) {
try {
session.saveOrUpdate(student);
} catch (Exception e) {
e.printStackTrace();
}
}
public void updateStudent(Student student) {
try {
session.saveOrUpdate(student);
session.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
public Student listStudentById(int Id) {
Student student = null;
try {
student = (Student) session.get(Student.class, Id);
} catch (Exception e) {
}
return student;
}
}
Student.java
package com.struts2hibernatepagination.hibernate;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name = "student")
public class Student {
#Id
#GeneratedValue
#Column(name = "id")
private int id;
#Column(name = "last_name")
private String lastName;
#Column(name = "first_name")
private String firstName;
private int marks;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<package name="default" extends="hibernate-default">
<action name="addStudent" method="execute"
class="com.struts2hibernatepagination.action.AddStudentAction">
<interceptor-ref name="defaultStackHibernateStrutsValidation">
<param name="validation.excludeMethods">listStudents</param>
<param name="validation.excludeMethods">fetchStudentList</param>
</interceptor-ref>
<result name="success" type="redirect">
listStudents
</result>
<result name="input">/student.jsp</result>
</action>
<action name="editStudent"
class="com.struts2hibernatepagination.action.AddStudentAction"
method="editStudent">
<interceptor-ref name="basicStackHibernate" />
<result name="success">/student.jsp</result>
</action>
</package>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">admin12345</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.1.3:3306/nupur</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="com.struts2hibernatepagination.hibernate.Student" />
</session-factory>
</hibernate-configuration>
student.jsp
<%# page contentType="text/html; charset=UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Hello World</title>
<s:head />
</head>
<body>
Search
<s:form action="addStudent">
<s:actionerror />
<s:textfield name="firstName" label="First Name" />
<s:textfield name="lastName" label="Last Name" />
<s:textfield name="marks" label="Marks" />
<s:submit value="Save" />
<hr />
<table border="2">
<tr bgcolor="cyan">
<th><u>First Name</u></th>
<th><u>Last Name</u></th>
<th><u>Marks</u></th>
<th><u>Edit</u></th>
<th><u>Delete</u></th>
</tr>
<s:iterator value="students">
<tr>
<td><s:property value="firstName" /></td>
<td><s:property value="lastName" /></td>
<td><s:property value="marks" /></td>
<td><s:url id="editURL" action="editStudent">
<s:param name="id" value="%{id}"></s:param>
</s:url> <s:a href="%{editURL}">Edit</s:a></td>
<td><s:url id="deleteURL" action="deleteStudent">
<s:param name="id" value="%{id}"></s:param>
</s:url> <s:a href="%{deleteURL}">Delete</s:a></td>
</tr>
</s:iterator>
</table>
</s:form>
<s:form action="fetchStudentList">
<s:submit value="Show"></s:submit>
</s:form>
</body>
</html>
Please help Me... As I tried all ways whether it is of using session.flush() or deleting #GeneratedValue annotation
Id not coming in addStudent()
public String editStudent() {
HttpServletRequest request = (HttpServletRequest) ActionContext
.getContext().get(ServletActionContext.HTTP_REQUEST);
dao.listStudentById(Integer.parseInt((request.getParameter("id")))); return SUCCESS;
} //AddStudetnAction class method
public void addStudent(Student student) {
try {
System.out.println(student.getId() + "In DAO addStudent");// returning null
session.saveOrUpdate(student);
} catch (Exception e) {
e.printStackTrace();
}
//StudentDAO class method
The addStudent() of StudentDAOis getting null value for id, how can I set the id for addStudent() so that it calls update method instead of save
After spending nearly 2-3 days, I got the solution, I mentioned the <s:hidden> tag in my JSP from where I am passing the id as
<s:hidden name="id" /> , in order to update the Hibernate should get the id and if it fails to do so it will call save method.
if you want to update record you need to have it's id, SaveOrUpdate looks for the id if it is in your table then it updates , if it is not then its add a new record.
I am not sure how you create your Student but if you want to update you need to get the object from the database to have the id ( or if you know the id just set it) then update what you want while the object is in the persistance context and flush.
Here is my edit method.
StudentAction.java
public String edit()
{
HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
student = studentDAO.listStudentsById(Long.parseLong(request.getParameter("id")));
return SUCCESS;
}
StudentList.jsp
<s:url id="editURL" action="edit">
<s:param name="id" value="%{id}"></s:param>
</s:url>
<s:a href="%{editURL}">Edit</s:a>
The s:form tag doesn't let you to use any parameter in the URL, but you can use hidden input field. s:form tag defaults to POST method.
<s:form action="addStudent">
<s:actionerror />
<s:textfield name="firstName" label="First Name" />
<s:textfield name="lastName" label="Last Name" />
<s:textfield name="marks" label="Marks" />
<s:hidden name="id"/>
<s:submit value="Save" />
...
</s:form>
I'm quite new to spring boot and I'd like to create a multiple datasource for my project.
Here is my current case. I have two packages for entity for multiple database. Let's say
com.test.entity.db.mysql ; for entities that belong to MySql
com.test.entity.db.h2 ; for entities that belong to H2 Databases
So, currently I have two entities class
UserMySql.java
#Entity
#Table(name="usermysql")
public class UserMysql{
#Id
#GeneratedValue
public int id;
public String name;
}
UserH2.java
#Entity
#Table(name="userh2")
public class Userh2 {
#Id
#GeneratedValue
public int id;
public String name;
}
I'd like to achieve a configuration where if I create user from UserMySql, it will be saved to MySql Database, and if I create user from Userh2 it will be saved to H2 Databases. So, I also have two DBConfig, let's say MySqlDbConfig and H2DbConfig.
(com.test.model is package where I'll put my Repositories class. It will be defined below)
MySqlDbConfig.java
#Configuration
#EnableJpaRepositories(
basePackages="com.test.model",
entityManagerFactoryRef = "mysqlEntityManager")
public class MySqlDBConfig {
#Bean
#Primary
#ConfigurationProperties(prefix="datasource.test.mysql")
public DataSource mysqlDataSource(){
return DataSourceBuilder
.create()
.build();
}
#Bean(name="mysqlEntityManager")
public LocalContainerEntityManagerFactoryBean mySqlEntityManagerFactory(
EntityManagerFactoryBuilder builder){
return builder.dataSource(mysqlDataSource())
.packages("com.test.entity.db.mysql")
.build();
}
}
H2DbConfig.java
#Configuration
#EnableJpaRepositories(
entityManagerFactoryRef = "h2EntityManager")
public class H2DbConfig {
#Bean
#ConfigurationProperties(prefix="datasource.test.h2")
public DataSource h2DataSource(){
return DataSourceBuilder
.create()
.driverClassName("org.h2.Driver")
.build();
}
#Bean(name="h2EntityManager")
public LocalContainerEntityManagerFactoryBean h2EntityManagerFactory(
EntityManagerFactoryBuilder builder){
return builder.dataSource(h2DataSource())
.packages("com.test.entity.db.h2")
.build();
}
}
My application.properties file
#DataSource settings for mysql
datasource.test.mysql.jdbcUrl = jdbc:mysql://127.0.0.1:3306/test
datasource.test.mysql.username = root
datasource.test.mysql.password = root
datasource.test.mysql.driverClassName = com.mysql.jdbc.Driver
#DataSource settings for H2
datasource.test.h2.jdbcUrl = jdbc:h2:~/test
datasource.test.h2.username = sa
# DataSource settings: set here configurations for the database connection
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.validation-query=SELECT 1
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate settings are prefixed with spring.jpa.hibernate.*
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.hibernate.show_sql = true
spring.jpa.hibernate.format_sql = true
server.port=8080
endpoints.shutdown.enabled=false
And then for crud I have UserMySqlDao and UserH2Dao
UserMySqlDao.java
#Transactional
#Repository
public interface UserMysqlDao extends CrudRepository<UserMysql, Integer>{
public UserMysql findByName(String name);
}
UserH2Dao.java
#Transactional
#Repositories
public interface UserH2Dao extends CrudRepository<Userh2, Integer>{
public Userh2 findByName(String name);
}
And for last, I have an UserController as endpoint to access my service
UserController.java
#Controller
#RequestMapping("/user")
public class UserController {
#Autowired
private UserMysqlDao userMysqlDao;
#Autowired
private UserH2Dao userH2Dao;
#RequestMapping("/createM")
#ResponseBody
public String createUserMySql(String name){
UserMysql user = new UserMysql();
try{
user.name = name;
userMysqlDao.save(user);
return "Success creating user with Id: "+user.id;
}catch(Exception ex){
return "Error creating the user: " + ex.toString();
}
}
#RequestMapping("/createH")
#ResponseBody
public String createUserH2(String name){
Userh2 user = new Userh2();
try{
user.name = name;
userH2Dao.save(user);
return "Success creating user with Id: "+user.id;
}catch(Exception ex){
return "Error creating the user: " + ex.toString();
}
}
}
Application.java
#Configuration
#EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
#EntityScan(basePackages="com.test.entity.db")
#ComponentScan
public class Application {
public static void main(String[] args) {
System.out.println("Entering spring boot");
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.print(beanName);
System.out.print(" ");
}
System.out.println("");
}
}
With this configuration my Spring boot run well, but when I access
http://localhost/user/createM?name=myname it writes an exception
Error creating the user: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement
I've googling around and haven't got a solution yet. Any ideas why this exception occurs? And is this the best way to implement multiple datasource to implement my case above? I'm open to full refactor if needed.
Thanks
I think you can find it usefull
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources
It shows how to define multiple datasources & assign one of them as primary.
Here is a rather full example, also contains distributes transactions - if you need it.
http://fabiomaffioletti.me/blog/2014/04/15/distributed-transactions-multiple-databases-spring-boot-spring-data-jpa-atomikos/
What you need is to create 2 configuration classes, separate the model/repository packages etc to make the config easy.
Also, in above example, it creates the data sources manually. You can avoid this using the method on spring doc, with #ConfigurationProperties annotation. Here is an example of this:
http://xantorohara.blogspot.com.tr/2013/11/spring-boot-jdbc-with-multiple.html
Hope these helps.
I faced same issue few days back, I followed the link mentioned below and I could able to overcome the problem
http://www.baeldung.com/spring-data-jpa-multiple-databases
I solved the problem (How to connect multiple database using spring and Hibernate) in this way, I hope it will help :)
NOTE: I have added the relevant code, kindly make the dao with the help of impl I used in the below mentioned code.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>MultipleDatabaseConnectivityInSpring</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml
</param-value>
</context-param>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="localPersistenceUnitOne"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>in.india.entities.CustomerDetails</class>
<exclude-unlisted-classes />
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.jdbc.batch_size" value="0" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/shankar?sslmode=require" />
<property name="hibernate.connection.username" value="username" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
<persistence-unit name="localPersistenceUnitTwo"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>in.india.entities.CompanyDetails</class>
<exclude-unlisted-classes />
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.jdbc.batch_size" value="0" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/shankarTwo?sslmode=require" />
<property name="hibernate.connection.username" value="username" />
<property name="hibernate.connection.password" value="password" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
dispatcher-servlet
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
default-autowire="byName"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Configure messageSource -->
<mvc:annotation-driven />
<context:component-scan base-package="in.india.*" />
<bean id="messageResource"
class="org.springframework.context.support.ResourceBundleMessageSource"
autowire="byName">
<property name="basename" value="messageResource"></property>
</bean>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="entityManagerFactoryOne"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
autowire="constructor">
<property name="persistenceUnitName" value="localPersistenceUnitOne" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource"
autowire="byName">
<property name="basename" value="messageResource" />
</bean>
<bean id="entityManagerFactoryTwo"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
autowire="constructor">
<property name="persistenceUnitName" value="localPersistenceUnitTwo" />
</bean>
<bean id="manager1" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryOne" />
</bean>
<bean id="manager2" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryTwo" />
</bean>
<tx:annotation-driven transaction-manager="manager1" />
<tx:annotation-driven transaction-manager="manager2" />
<!-- declare dependies here -->
<bean class="in.india.service.dao.impl.CustomerServiceImpl" />
<bean class="in.india.service.dao.impl.CompanyServiceImpl" />
<!-- Configure MVC annotations -->
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</beans>
java class to persist into one database
package in.india.service.dao.impl;
import in.india.entities.CompanyDetails;
import in.india.service.CompanyService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;
public class CompanyServiceImpl implements CompanyService {
#PersistenceContext(unitName = "entityManagerFactoryTwo")
EntityManager entityManager;
#Transactional("manager2")
#Override
public boolean companyService(CompanyDetails companyDetails) {
boolean flag = false;
try
{
entityManager.persist(companyDetails);
flag = true;
}
catch (Exception e)
{
flag = false;
}
return flag;
}
}
java class to persist in another database
package in.india.service.dao.impl;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;
import in.india.entities.CustomerDetails;
import in.india.service.CustomerService;
public class CustomerServiceImpl implements CustomerService {
#PersistenceContext(unitName = "localPersistenceUnitOne")
EntityManager entityManager;
#Override
#Transactional(value = "manager1")
public boolean customerService(CustomerDetails companyData) {
boolean flag = false;
entityManager.persist(companyData);
return flag;
}
}
customer.jsp
<%#page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<h1>SpringWithMultipleDatabase's</h1>
</center>
<form:form method="GET" action="addCustomer.htm" modelAttribute="customerBean" >
<table>
<tr>
<td><form:label path="firstName">First Name</form:label></td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td><form:label path="lastName">Last Name</form:label></td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td><form:label path="emailId">Email Id</form:label></td>
<td><form:input path="emailId" /></td>
</tr>
<tr>
<td><form:label path="profession">Profession</form:label></td>
<td><form:input path="profession" /></td>
</tr>
<tr>
<td><form:label path="address">Address</form:label></td>
<td><form:input path="address" /></td>
</tr>
<tr>
<td><form:label path="age">Age</form:label></td>
<td><form:input path="age" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
</form:form>
</body>
</html>
company.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>ScheduleJobs</title>
</head>
<body>
<center><h1>SpringWithMultipleDatabase's</h1></center>
<form:form method="GET" action="addCompany.htm" modelAttribute="companyBean" >
<table>
<tr>
<td><form:label path="companyName">Company Name</form:label></td>
<td><form:input path="companyName" /></td>
</tr>
<tr>
<td><form:label path="companyStrength">Company Strength</form:label></td>
<td><form:input path="companyStrength" /></td>
</tr>
<tr>
<td><form:label path="companyLocation">Company Location</form:label></td>
<td><form:input path="companyLocation" /></td>
</tr>
<tr>
<td>
<input type="submit" value="Submit"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
<center><h1>Multiple Database Connectivity In Spring sdfsdsd</h1></center>
<a href='customerRequest.htm'>Click here to go on Customer page</a>
<br>
<a href='companyRequest.htm'>Click here to go on Company page</a>
</body>
</html>
success.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>ScheduleJobs</title>
</head>
<body>
<center><h1>SpringWithMultipleDatabase</h1></center>
<b>Successfully Saved</b>
</body>
</html>
CompanyController
package in.india.controller;
import in.india.bean.CompanyBean;
import in.india.entities.CompanyDetails;
import in.india.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class CompanyController {
#Autowired
CompanyService companyService;
#RequestMapping(value = "/companyRequest.htm", method = RequestMethod.GET)
public ModelAndView addStudent(ModelMap model) {
CompanyBean companyBean = new CompanyBean();
model.addAttribute(companyBean);
return new ModelAndView("company");
}
#RequestMapping(value = "/addCompany.htm", method = RequestMethod.GET)
public ModelAndView companyController(#ModelAttribute("companyBean") CompanyBean companyBean, Model model) {
CompanyDetails companyDetails = new CompanyDetails();
companyDetails.setCompanyLocation(companyBean.getCompanyLocation());
companyDetails.setCompanyName(companyBean.getCompanyName());
companyDetails.setCompanyStrength(companyBean.getCompanyStrength());
companyService.companyService(companyDetails);
return new ModelAndView("success");
}
}
CustomerController
package in.india.controller;
import in.india.bean.CustomerBean;
import in.india.entities.CustomerDetails;
import in.india.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class CustomerController {
#Autowired
CustomerService customerService;
#RequestMapping(value = "/customerRequest.htm", method = RequestMethod.GET)
public ModelAndView addStudent(ModelMap model) {
CustomerBean customerBean = new CustomerBean();
model.addAttribute(customerBean);
return new ModelAndView("customer");
}
#RequestMapping(value = "/addCustomer.htm", method = RequestMethod.GET)
public ModelAndView customerController(#ModelAttribute("customerBean") CustomerBean customer, Model model) {
CustomerDetails customerDetails = new CustomerDetails();
customerDetails.setAddress(customer.getAddress());
customerDetails.setAge(customer.getAge());
customerDetails.setEmailId(customer.getEmailId());
customerDetails.setFirstName(customer.getFirstName());
customerDetails.setLastName(customer.getLastName());
customerDetails.setProfession(customer.getProfession());
customerService.customerService(customerDetails);
return new ModelAndView("success");
}
}
CompanyDetails Entity
package in.india.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
#Entity
#Table(name = "company_details")
public class CompanyDetails {
#Id
#SequenceGenerator(name = "company_details_seq", sequenceName = "company_details_seq", initialValue = 1, allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "company_details_seq")
#Column(name = "company_details_id")
private Long companyDetailsId;
#Column(name = "company_name")
private String companyName;
#Column(name = "company_strength")
private Long companyStrength;
#Column(name = "company_location")
private String companyLocation;
public Long getCompanyDetailsId() {
return companyDetailsId;
}
public void setCompanyDetailsId(Long companyDetailsId) {
this.companyDetailsId = companyDetailsId;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Long getCompanyStrength() {
return companyStrength;
}
public void setCompanyStrength(Long companyStrength) {
this.companyStrength = companyStrength;
}
public String getCompanyLocation() {
return companyLocation;
}
public void setCompanyLocation(String companyLocation) {
this.companyLocation = companyLocation;
}
}
CustomerDetails Entity
package in.india.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
#Entity
#Table(name = "customer_details")
public class CustomerDetails {
#Id
#SequenceGenerator(name = "customer_details_seq", sequenceName = "customer_details_seq", initialValue = 1, allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "customer_details_seq")
#Column(name = "customer_details_id")
private Long customerDetailsId;
#Column(name = "first_name ")
private String firstName;
#Column(name = "last_name ")
private String lastName;
#Column(name = "email_id")
private String emailId;
#Column(name = "profession")
private String profession;
#Column(name = "address")
private String address;
#Column(name = "age")
private int age;
public Long getCustomerDetailsId() {
return customerDetailsId;
}
public void setCustomerDetailsId(Long customerDetailsId) {
this.customerDetailsId = customerDetailsId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Update 2018-01-07 with Spring Boot 1.5.8.RELEASE
If you want to know how to config it, how to use it, and how to control transaction. I may have answers for you.
You can see the runnable example and some explanation in https://www.surasint.com/spring-boot-with-multiple-databases-example/
I copied some code here.
First you have to set application.properties like this
#Database
database1.datasource.url=jdbc:mysql://localhost/testdb
database1.datasource.username=root
database1.datasource.password=root
database1.datasource.driver-class-name=com.mysql.jdbc.Driver
database2.datasource.url=jdbc:mysql://localhost/testdb2
database2.datasource.username=root
database2.datasource.password=root
database2.datasource.driver-class-name=com.mysql.jdbc.Driver
Then define them as providers (#Bean) like this:
#Bean(name = "datasource1")
#ConfigurationProperties("database1.datasource")
#Primary
public DataSource dataSource(){
return DataSourceBuilder.create().build();
}
#Bean(name = "datasource2")
#ConfigurationProperties("database2.datasource")
public DataSource dataSource2(){
return DataSourceBuilder.create().build();
}
Note that I have #Bean(name="datasource1") and #Bean(name="datasource2"), then you can use it when we need datasource as #Qualifier("datasource1") and #Qualifier("datasource2") , for example
#Qualifier("datasource1")
#Autowired
private DataSource dataSource;
If you do care about transaction, you have to define DataSourceTransactionManager for both of them, like this:
#Bean(name="tm1")
#Autowired
#Primary
DataSourceTransactionManager tm1(#Qualifier ("datasource1") DataSource datasource) {
DataSourceTransactionManager txm = new DataSourceTransactionManager(datasource);
return txm;
}
#Bean(name="tm2")
#Autowired
DataSourceTransactionManager tm2(#Qualifier ("datasource2") DataSource datasource) {
DataSourceTransactionManager txm = new DataSourceTransactionManager(datasource);
return txm;
}
Then you can use it like
#Transactional //this will use the first datasource because it is #primary
or
#Transactional("tm2")
This should be enough. See example and detail in the link above.
Use multiple datasource or realizing the separation of reading & writing.
you must have a knowledge of Class AbstractRoutingDataSource which support dynamic datasource choose.
Here is my datasource.yaml and I figure out how to resolve this case. You can refer to this project spring-boot + quartz. Hope this will help you.
dbServer:
default: localhost:3306
read: localhost:3306
write: localhost:3306
datasource:
default:
type: com.zaxxer.hikari.HikariDataSource
pool-name: default
continue-on-error: false
jdbc-url: jdbc:mysql://${dbServer.default}/schedule_job?useSSL=true&verifyServerCertificate=false&useUnicode=true&characterEncoding=utf8
username: root
password: lh1234
connection-timeout: 30000
connection-test-query: SELECT 1
maximum-pool-size: 5
minimum-idle: 2
idle-timeout: 600000
destroy-method: shutdown
auto-commit: false
read:
type: com.zaxxer.hikari.HikariDataSource
pool-name: read
continue-on-error: false
jdbc-url: jdbc:mysql://${dbServer.read}/schedule_job?useSSL=true&verifyServerCertificate=false&useUnicode=true&characterEncoding=utf8
username: root
password: lh1234
connection-timeout: 30000
connection-test-query: SELECT 1
maximum-pool-size: 5
minimum-idle: 2
idle-timeout: 600000
destroy-method: shutdown
auto-commit: false
write:
type: com.zaxxer.hikari.HikariDataSource
pool-name: write
continue-on-error: false
jdbc-url: jdbc:mysql://${dbServer.write}/schedule_job?useSSL=true&verifyServerCertificate=false&useUnicode=true&characterEncoding=utf8
username: root
password: lh1234
connection-timeout: 30000
connection-test-query: SELECT 1
maximum-pool-size: 5
minimum-idle: 2
idle-timeout: 600000
destroy-method: shutdown
auto-commit: false
Using two datasources you need their own transaction managers.
#Configuration
public class MySqlDBConfig {
#Bean
#Primary
#ConfigurationProperties(prefix="datasource.test.mysql")
public DataSource mysqlDataSource(){
return DataSourceBuilder
.create()
.build();
}
#Bean("mysqlTx")
public DataSourceTransactionManager mysqlTx() {
return new DataSourceTransactionManager(mysqlDataSource());
}
// same for another DS
}
And then use it accordingly within #Transaction
#Transactional("mysqlTx")
#Repository
public interface UserMysqlDao extends CrudRepository<UserMysql, Integer>{
public UserMysql findByName(String name);
}
Thanks all for your help but it is not complicated as it seems; almost everything is handled internally by SpringBoot.
In my case I want to use Mysql and Mongodb and the solution was to use EnableMongoRepositories and EnableJpaRepositories annotations on to my application class.
#SpringBootApplication
#EnableTransactionManagement
#EnableMongoRepositories(includeFilters = #ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = MongoRepository))
#EnableJpaRepositories(excludeFilters = #ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = MongoRepository))
class TestApplication { ...
NB: All mysql entities have to extend JpaRepository and mongo enities have to extend MongoRepository.
The datasource configs are straight forward as presented by spring documentation:
//mysql db config
spring.datasource.url= jdbc:mysql://localhost:3306/tangio
spring.datasource.username=test
spring.datasource.password=test
#mongodb config
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=tangio
spring.data.mongodb.username=tangio
spring.data.mongodb.password=tangio
spring.data.mongodb.repositories.enabled=true
once you start work with jpa and some driver is in your class path spring boot right away puts it inside as your data source (e.g h2 )
for using the defult data source therefore u will need only to define
spring.datasource.url= jdbc:mysql://localhost:3306/
spring.datasource.username=test
spring.datasource.password=test
if we go one step farther and u want to use two
I would reccomend to use two data sources such as explained here :
Spring Boot Configure and Use Two DataSources
MySqlBDConfig.java
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(basePackages = "PACKAGE OF YOUR CRUDS USING MYSQL DATABASE",entityManagerFactoryRef = "mysqlEmFactory" ,transactionManagerRef = "mysqlTransactionManager")
public class MySqlBDConfig{
#Autowired
private Environment env;
#Bean(name="mysqlProperities")
#ConfigurationProperties(prefix="spring.mysql")
public DataSourceProperties mysqlProperities(){
return new DataSourceProperties();
}
#Bean(name="mysqlDataSource")
public DataSource interfaceDS(#Qualifier("mysqlProperities")DataSourceProperties dataSourceProperties){
return dataSourceProperties.initializeDataSourceBuilder().build();
}
#Primary
#Bean(name="mysqlEmFactory")
public LocalContainerEntityManagerFactoryBean mysqlEmFactory(#Qualifier("mysqlDataSource")DataSource mysqlDataSource,EntityManagerFactoryBuilder builder){
return builder.dataSource(mysqlDataSource).packages("PACKAGE OF YOUR MODELS").build();
}
#Bean(name="mysqlTransactionManager")
public PlatformTransactionManager mysqlTransactionManager(#Qualifier("mysqlEmFactory")EntityManagerFactory factory){
return new JpaTransactionManager(factory);
}
}
H2DBConfig.java
#Configuration
#EnableTransactionManagement
#EnableJpaRepositories(basePackages = "PACKAGE OF YOUR CRUDS USING MYSQL DATABASE",entityManagerFactoryRef = "dsEmFactory" ,transactionManagerRef = "dsTransactionManager")
public class H2DBConfig{
#Autowired
private Environment env;
#Bean(name="dsProperities")
#ConfigurationProperties(prefix="spring.h2")
public DataSourceProperties dsProperities(){
return new DataSourceProperties();
}
#Bean(name="dsDataSource")
public DataSource dsDataSource(#Qualifier("dsProperities")DataSourceProperties dataSourceProperties){
return dataSourceProperties.initializeDataSourceBuilder().build();
}
#Bean(name="dsEmFactory")
public LocalContainerEntityManagerFactoryBean dsEmFactory(#Qualifier("dsDataSource")DataSource dsDataSource,EntityManagerFactoryBuilder builder){
LocalContainerEntityManagerFactoryBean em = builder.dataSource(dsDataSource).packages("PACKAGE OF YOUR MODELS").build();
HibernateJpaVendorAdapter ven = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(ven);
HashMap<String, Object> prop = new HashMap<>();
prop.put("hibernate.dialect", env.getProperty("spring.jpa.properties.hibernate.dialect"));
prop.put("hibernate.show_sql", env.getProperty("spring.jpa.show-sql"));
em.setJpaPropertyMap(prop);
em.afterPropertiesSet();
return em;
}
#Bean(name="dsTransactionManager")
public PlatformTransactionManager dsTransactionManager(#Qualifier("dsEmFactory")EntityManagerFactory factory){
return new JpaTransactionManager(factory);
}
}
application.properties
#---mysql DATASOURCE---
spring.mysql.driverClassName = com.mysql.jdbc.Driver
spring.mysql.url = jdbc:mysql://127.0.0.1:3306/test
spring.mysql.username = root
spring.mysql.password = root
#----------------------
#---H2 DATASOURCE----
spring.h2.driverClassName = org.h2.Driver
spring.h2.url = jdbc:h2:file:~/test
spring.h2.username = root
spring.h2.password = root
#---------------------------
#------JPA-----
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
Application.java
#SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext ac=SpringApplication.run(KeopsSageInvoiceApplication.class, args);
UserMysqlDao userRepository = ac.getBean(UserMysqlDao.class)
//for exemple save a new user using your repository
userRepository.save(new UserMysql());
}
}
I am new to struts and was trying to save some values to DB (mysql) from jsp page using struts and hibernate.
But, the application is saving null value every time and auto increment ID is increasing.
My Database structure. :
Table Name | osdetail
-------------------------
Columns | os_name,
| os_version,
| id,
| created,
| notes.
The index.jsp page
<%# page contentType="text/html; charset=UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>OS Manager - Struts2 Hibernate Example</title>
</head>
<body>
<h1>OS Manager</h1>
<s:actionerror/>
<s:form action="add" method="post">
<s:textfield name="osdetail.OSname" label="name"/>
<s:textfield name="osdetail.OSversion" label="version"/>
<s:textfield name="osdetail.OSnotes" label="notes"/>
<s:submit value="Add OS Details" align="center"/>
</s:form>
<h2>OS Details</h2>
<table>
<tr>
<th>OS Name</th>
<th>OS Version</th>
<th>OS Notes</th>
</tr>
<s:iterator value="osdetails_list" var="osdetail">
<tr>
<td><s:property value="OSname"/></td>
<td><s:property value="OSversion"/></td>
<td><s:property value="OSnotes"/></td>
</tr>
</s:iterator>
</table>
</body>
</html>
My View : OSAction.java
package net.ajeet.os.view;
import java.util.List;
import net.ajeet.os.controller.OSManager;
import net.ajeet.os.model.OSDetail;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class OSAction extends ActionSupport implements ModelDriven<OSDetail> {
private static final long serialVersionUID = 9149826260758390091L;
private OSDetail osdetail= new OSDetail();
private List<OSDetail> osdetails_list;
private Long id;
private OSManager linkController= new OSManager();
#Override
public OSDetail getModel() {
return osdetail;
}
public OSAction() {
linkController = new OSManager();
}
public String execute() {
this.osdetails_list = linkController.list();
return SUCCESS;
}
public String add() {
System.out.println("this is oS detail get ID"+osdetail.getId());
try {
//linkController.add(getOSDetail());
linkController.add(osdetail);
System.out.println("this is oS detail after add "+getOSDetail());
} catch (Exception e) {
e.printStackTrace();
}
this.osdetails_list = linkController.list();
return SUCCESS;
}
public String delete() {
linkController.delete(getId());
return SUCCESS;
}
public OSDetail getOSDetail() {
return osdetail;
}
public List<OSDetail> getOSDetail_list() {
return osdetails_list;
}
public void setOSDetail(OSDetail osdetail) {
this.osdetail = osdetail;
}
public void setOSDetail_list(List<OSDetail> osdetails_list) {
this.osdetails_list = osdetails_list;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
My Model: OSDetail.java
package net.ajeet.os.model;
import java.io.Serializable;
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="osdetail")
public class OSDetail implements Serializable{
private static final long serialVersionUID = -8767337896773261247L;
private Long OSid;
private String OSname;
private String OSversion;
private String OSnotes;
private Date OScreated;
#Id
#GeneratedValue
#Column(name="id")
public Long getId() {
System.out.println("set os name is os id"+OSid);
return OSid;
}
#Column(name="os_name")
public String getOS_name() {
return OSname;
}
#Column(name="os_version")
public String getOS_version() {
return OSversion;
}
#Column(name="notes")
public String getNotes() {
return OSnotes;
}
#Column(name="created")
public Date getCreated() {
return OScreated;
}
public void setId(Long OSid) {
this.OSid = OSid;
}
public void setOS_name(String OSname) {
this.OSname = OSname;
}
public void setOS_version(String OSversion) {
this.OSversion = OSversion;
}
public void setNotes(String OSnotes) {
this.OSnotes = OSnotes;
}
public void setCreated(Date OScreated) {
this.OScreated = OScreated;
}
My Contoller :OSManager.java
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import net.ajeet.os.model.OSDetail;
import net.ajeet.os.util.HibernateUtil;
public class OSManager extends HibernateUtil {
public OSDetail add(OSDetail osdetail) {
System.out.println("value of the os in OSManager"+osdetail.getId());
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(osdetail);
session.getTransaction().commit();
return osdetail;
}
public OSDetail delete(Long id) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
OSDetail osdetail = (OSDetail) session.load(OSDetail.class, id);
if(null != osdetail) {
session.delete(osdetail);
}
session.getTransaction().commit();
return osdetail;
}
#SuppressWarnings("unchecked")
public List<OSDetail> list() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<OSDetail> osdetails_list = null;
try {
osdetails_list = (List<OSDetail>)session.createQuery("from OSDetail").list();
} catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
}
session.getTransaction().commit();
return osdetails_list;
}
}
The values saved in DB are always null...except the ID..Please help
Changed the Action...updated getter/setter
package net.ajeet.os.view;
import java.util.List;
import net.ajeet.os.controller.OSManager;
import net.ajeet.os.model.OSDetail;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class OSAction extends ActionSupport {
private static final long serialVersionUID = 9149826260758390091L;
public OSDetail osdetail= new OSDetail();
private List<OSDetail> osdetails_list;
public OSDetail getOsdetail() {
return osdetail;
}
public void setOsdetail(OSDetail osdetail) {
this.osdetail = osdetail;
}
private Long id;
private OSManager linkController= new OSManager();
/* #Override
public OSDetail getModel() {
return osdetail;
}*/
public OSAction() {
linkController = new OSManager();
}
public String execute() {
this.osdetails_list = linkController.list();
return SUCCESS;
}
public String add() {
try {
linkController.add(getOsdetail());
//linkController.add(osdetail);
} catch (Exception e) {
e.printStackTrace();
}
this.osdetails_list = linkController.list();
return SUCCESS;
}
public String delete() {
linkController.delete(getid());
return SUCCESS;
}
public List<OSDetail> getOsdetails_list() {
return osdetails_list;
}
public void setOsdetails_list(List<OSDetail> osdetails_list) {
this.osdetails_list = osdetails_list;
}
public Long getid() {
return id;
}
public void setid(Long id) {
this.id = id;
}
}
Corrected OSDetail.java, automatically created getter/setter.
package net.ajeet.os.model;
import java.io.Serializable;
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name="osdetail")
public class OSDetail implements Serializable{
private static final long serialVersionUID = -8767337896773261247L;
private Long OSid;
private String OSname;
private String OSversion;
private String OSnotes;
private Date OScreated;
#Id
#GeneratedValue
#Column(name="id")
public Long getOSid() {
return OSid;
}
public void setOSid(Long oSid) {
OSid = oSid;
}
#Column(name="os_name")
public String getOSname() {
return OSname;
}
public void setOSname(String oSname) {
OSname = oSname;
}
#Column(name="os_version")
public String getOSversion() {
return OSversion;
}
public void setOSversion(String oSversion) {
OSversion = oSversion;
System.out.println("value of the os in OSversion in setter"+OSversion);
}
#Column(name="notes")
public String getOSnotes() {
return OSnotes;
}
public void setOSnotes(String oSnotes) {
OSnotes = oSnotes;
}
#Column(name="created")
public Date getOScreated() {
return OScreated;
}
public void setOScreated(Date oScreated) {
OScreated = oScreated;
}
}
Adding struts.xml to
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />
<package name="default" extends="struts-default" namespace="/">
<action name="add"
class="net.ajeet.os.view.OSAction" method="add">
<result name="success" type="chain">index</result>
<result name="input" type="chain">index</result>
</action>
<action name="index"
class="net.ajeet.os.view.OSAction">
<result name="success">index.jsp</result>
</action>
</package>
</struts>
And hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="connection.username">root</property>
<property name="connection.password">Asmita24</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">
org.hibernate.cache.NoCacheProvider
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
<mapping class="net.ajeet.os.model.OSDetail" />
</session-factory>
</hibernate-configuration>
Your getter function is wrongly named for the variable osdetail. It should be getOsdetail() instead of getOSDetail(). That is the reason your values from the form are not set and the variable osdetail has blank values. Try changing it. Same goes for the setter method, it should be setOsdetail(). Also, to prevent making such mistake in fututre, you can generate your getter and setter functions automatically from eclipse instead of manually creating it.
I don't see anywhere in your OSAction the code to read the values from the jsp. And that is the reason why I think the values are going in as null in the DB.
I suppose it'll work if you get the details from the jsp and set it to osdetail before calling linkController.add(osdetail);.
I am not too well versed with ActionSupport but I think you can read the values from the jsp using getText() method...
Try adding these lines before linkController.add(osdetail); line...
osdetail.setOS_name(getText("osdetail.OSname"));
osdetail.setOS_version(getText("osdetail.OSversion"));
osdetail.setNotes(getText("osdetail.OSnotes"));