java – 如何处理应用BeanValidation关系约束的错误?

前端之家收集整理的这篇文章主要介绍了java – 如何处理应用BeanValidation关系约束的错误?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 Spring Hibernate 4.1应用程序中创建oneToMany关系时遇到了一些问题
这是我的实体类.每个USER_ROLE记录都有FK到USER记录.我在互联网上找不到任何有用的东西.
@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;
    }
}

这是我的堆栈跟踪中的第一个异常:

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)

解决方法

我没有使用Spring,我正在使用hibernate 4.1.8并且正在添加到我的hibernate.cfg.xml:
<property name="javax.persistence.validation.mode">none</property>
原文链接:https://www.f2er.com/java/128310.html

猜你在找的Java相关文章