Avatar
Jakub
Neregistrovaný
Avatar
Jakub:

Čaute,

mám takový problém stavím aplikaci na springu + hibernate a databázi mám oraclovskou. Snažím se vytvořit základní architekturu aplikace, ale bohužel jsem narazil na problém při ukládání dat z formuláře do DB. Vím že je zde problém někde v PK a sekvencích, ale už jsem úplně zoufalý. Appka nehází žádné chyby nic, jen se prostě neprovede INSERT do DB.

Tady jsou kódy:

User.java

package com.beingjavaguys.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "USER2")
public class User {

        @Id
        @SequenceGenerator(name="gen", sequenceName="SEQ_USER2")
        @GeneratedValue(generator="gen")
        @Column(name = "user_id", nullable = false)
        private int id;

        @Column(name = "first_name")
        private String firstName;

        @Column(name = "last_name")
        private String lastName;

        @Column(name = "gender")
        private String gender;

        @Column(name = "city")
        private String City;

        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        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 getGender() {
                return gender;
        }

        public void setGender(String gender) {
                this.gender = gender;
        }

        public String getCity() {
                return City;
        }

        public void setCity(String city) {
                City = city;
        }

}

HomeController.java

package com.beingjavaguys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.beingjavaguys.domain.User;
import com.beingjavaguys.service.UserService;

@Controller
public class HomeController {

        @Autowired
        private UserService userService;

        @RequestMapping("/register")
        public ModelAndView getRegisterForm(@ModelAttribute("user") User user,
                        BindingResult result) {
                ArrayList<String> gender = new ArrayList<String>();
                gender.add("Male");
                gender.add("Female");
                ArrayList<String> city = new ArrayList<String>();
                city.add("Delhi");
                city.add("Kolkata");
                city.add("Chennai");
                city.add("Bangalore");
                Map<String, Object> model = new HashMap<String, Object>();
                model.put("gender", gender);
                model.put("city", city);
                System.out.println("Register Form");
                return new ModelAndView("Register", "model", model);
        }

        @RequestMapping("/saveUser")
        public ModelAndView saveUserData(@ModelAttribute("user") User user,
                        BindingResult result) {
                userService.addUser(user);
                System.out.println("Save User Data");
                return new ModelAndView("redirect:/userList.html");
        }

        @RequestMapping("/userList")
        public ModelAndView getUserList() {
                Map<String, Object> model = new HashMap<String, Object>();
                model.put("user", userService.getUser());
                return new ModelAndView("UserDetails", model);

        }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

        <servlet>
                <servlet-name>dispatcher</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
                <servlet-name>dispatcher</servlet-name>
                <url-pattern>*.html</url-pattern>
        </servlet-mapping>

        <welcome-file-list>
                <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>

</web-app>

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

        <context:property-placeholder location="classpath:jdbc.properties" />
        <context:component-scan base-package="com.beingjavaguys" />

        <tx:annotation-driven transaction-manager="hibernateTransactionManager" />

        <bean id="jspViewResolver"
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="viewClass"
                        value="org.springframework.web.servlet.view.JstlView" />
                <property name="prefix" value="/WEB-INF/view/" />
                <property name="suffix" value=".jsp" />
        </bean>

        <bean id="dataSource"
                class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName" value="${database.driver}" />
                <property name="url" value="${database.url}" />
                <property name="username" value="${database.user}" />
                <property name="password" value="${database.password}" />
        </bean>

        <bean id="sessionFactory"
                class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="annotatedClasses">
                        <list>
                                <value>com.beingjavaguys.domain.User</value>
                        </list>
                </property>
                <property name="hibernateProperties">
                        <props>
                                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                                <prop key="hibernate.format_sql">true</prop>
                        </props>
                </property>
        </bean>

        <bean id="hibernateTransactionManager"
                class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory" ref="sessionFactory" />
        </bean>
</beans>

Předem díky za rady

 
Odpovědět 10.1.2014 10:46
Avatar
Silvinios
Redaktor
Avatar
Odpovídá na Jakub
Silvinios:

Jak vypadá UserService?

 
Nahoru Odpovědět 11.1.2014 11:56
Avatar
Jakub Kulhánek
Neregistrovaný
Avatar
Jakub Kulhánek:

Přesně takto:

package com.beingjavaguys.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.beingjavaguys.dao.UserDao;
import com.beingjavaguys.domain.User;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class UserServiceImpl implements UserService {

        @Autowired
        UserDao userDao;

        @Override
        public void addUser(User user) {
                userDao.saveUser(user);
        }

        @Override
        public List<User> getUser() {
                return userDao.getUser();
        }

}
 
Nahoru Odpovědět 12.1.2014 12:41
Avatar
Silvinios
Redaktor
Avatar
Odpovídá na Jakub Kulhánek
Silvinios:

Jak vypadá UserDao? :-)

Proč je v anotaci Transactional nastaveno readOnly na true, když se do databáze zapisuje?

 
Nahoru Odpovědět 15.1.2014 18:48
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 4 zpráv z 4.