Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with error applying BeanValidation relational constraints?

I've got some problems with making oneToMany relation in Spring + Hibernate 4.1 application This is my entities classes. Every USER_ROLE record has FK to USER record. I can't find anything useful on the Internet.

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

     long id;
     String login;
     String password;
     String name;
     String surname;
     GregorianCalendar birthDate;
     String email;
     GregorianCalendar joinDate;
     String randomKey;
     List<UserRole> userRoles = new ArrayList<UserRole>();

    public User(){ } //JavaBean Hibernate requirement

    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy = "increment")
    @Column(name="USER_ID", unique=true, nullable=false)
    public long getId() {
        return id;
    }

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

    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
    public List<UserRole> getUserRoles() {
        return userRoles;
    }

    public void setAccountRole(List<UserRole> aUserRoles) {
        for (UserRole role : aUserRoles) {
            this.addUserRole(role);
        }
    }

    public void addUserRole(UserRole aRole) {
        if (!this.userRoles.contains(aRole)) {
            aRole.setUser(this);
            this.userRoles.add(aRole);
        }
    }
}


@Entity
@Table(name = "USER_ROLE")
public class UserRole {

    Integer roleId;
    String role;
    User user;

    public UserRole() { }

//  public UserRole(String role) {
//      this.setRole(role);
//  }

    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment", strategy = "increment")
    @Column(name = "ROLE_ID", unique = true, nullable = false)
    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    @Basic
    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "USER_ID", nullable = false)
    public User getUser() {
        return user;
    }

    public void setUser(User aUser) {
        this.user = aUser;
    }
}

And this is first exception in my stack trace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: pl.rafalo235.encyklopedia.model.dao.UserDAO pl.rafalo235.encyklopedia.controllers.HomeController.userDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDAO': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.orm.hibernate4.LocalSessionFactoryBean pl.rafalo235.encyklopedia.model.dao.UserDAO.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/encyklopediaDispatcherServlet-servlet.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Error applying BeanValidation relational constraints
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1189)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1103)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1010)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4935)
    at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5262)
    at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5257)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
like image 616
Nabuchodonozor Avatar asked Jun 04 '12 15:06

Nabuchodonozor


People also ask

Which constraints on the relational database should not be violated?

Whenever we apply the above modification to the relation in the database, the constraints on the relational database should not get violated. 1. Domain constraint : Domain constraint gets violated only when a given value to the attribute does not appear in the corresponding domain or in case it is not of the appropriate datatype.

What is a constraint violation in SQL Server?

Any good SQL Server database will use constraints and other DRI in order to maintain integrity and increase performance. The violation of any constraints leads to an error, and it is rare to see this handled well. Let’s create a table that allows us to be able to make a couple of different constraint violations.

Are constraints the best way to ensure database integrity?

Once the handling of constraint errors within transactions has been tamed and understood, constraints will prove to be one of the best ways of guaranteeing the integrity of the data within a database. Phil Factor (real name withheld to protect the guilty), aka Database Mole, has 40 years of experience with database-intensive applications.

How to modify the default behavior of Java Bean Validation?

This is the default behavior which can be modified by implementing javax.validation.MessageInterpolator Java Bean Validation specification supports Unified Expression Language (JSR 341) for evaluating dynamic expression in the validation messages that's the reason we have to include 'javax.el' dependency in our projects.


3 Answers

I'm not using Spring and I'm using hibernate 4.1.8 and works adding to my hibernate.cfg.xml:

<property name="javax.persistence.validation.mode">none</property>
like image 185
Andrés Canavesi Avatar answered Oct 04 '22 22:10

Andrés Canavesi


Try add to hibernate config in Spring validation.mode - none:

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <props>
            ....
            <prop key="javax.persistence.validation.mode">none</prop>
        </props>
    </property>
</bean>
like image 42
crudo6 Avatar answered Oct 04 '22 21:10

crudo6


Using JPA and Hibernate works for me adding to persistence.xml:

<property name="javax.persistence.validation.mode" value="none"/>
like image 23
abdul Avatar answered Oct 04 '22 23:10

abdul