Multiple database with Spring+Hibernate+JPA

后端 未结 6 1531
别跟我提以往
别跟我提以往 2020-12-04 13:43

I\'m trying to configure Spring+Hibernate+JPA for work with two databases (MySQL and MSSQL).

My datasource-context.xml:



        
6条回答
  •  再見小時候
    2020-12-04 13:59

    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**
    
    
    
        MultipleDatabaseConnectivityInSpring
        
            index.jsp
        
         
            dispatcher
            org.springframework.web.servlet.DispatcherServlet
             1
        
         
            org.springframework.web.context.ContextLoaderListener
         
        
            contextConfigLocation
            
                /WEB-INF/dispatcher-servlet.xml
            
        
        
            dispatcher
            *.htm
        
        
            30
        
    
    
    
    **persistence.xml**
    
    
    
    
        
            org.hibernate.ejb.HibernatePersistence
            in.india.entities.CustomerDetails
            
            
                
                
                
                
                
                
                
                
            
        
        
            org.hibernate.ejb.HibernatePersistence
            in.india.entities.CompanyDetails
            
            
                
                
                
                
                
                
                
                
            
        
    
    
    
    **dispatcher-servlet**
    
    
    
    
        
    
        
        
        
            
        
    
        
            
                /WEB-INF/jsp/
            
            
                .jsp
            
        
    
    
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
        
    
        
    
        
        
    
        
        
        
        
    
    
    
    
    **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"%>
    
    
    
    
    Insert title here
    
    
        

    SpringWithMultipleDatabase's

    First Name
    Last Name
    Email Id
    Profession
    Address
    Age
    **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"%> ScheduleJobs

    SpringWithMultipleDatabase's

    Company Name
    Company Strength
    Company Location
    **index.jsp** <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> Home

    Multiple Database Connectivity In Spring sdfsdsd

    Click here to go on Customer page
    Click here to go on Company page **success.jsp** <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> ScheduleJobs

    SpringWithMultipleDatabase

    Successfully Saved **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; } }

提交回复
热议问题