Hibernate使用@MapKey注释存储地图

时间:2011-05-12 10:06:39

标签: java hibernate jpa

关注这些examples,因为hibernate中的@MapKey@MapKeyManyToMany已被弃用,我试图找出这种情况,没有成功:

taskAssignationState

protected Map<User, Boolean> usersState;
@ManyToMany(
            cascade={CascadeType.ALL},  
            mappedBy = "taskAssignationState",
            fetch=FetchType.LAZY
    )
    @MapKey(name = "user")
    public Map<User, Boolean> getUsersState() {
        return usersState;
    }
    public void setUsersState(Map<User, Boolean> usersState) {
        this.usersState = usersState;
    }

用户

@ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(name = "user_state_task_assignation", 
            joinColumns=@JoinColumn(name="username"),
            inverseJoinColumns=@JoinColumn(name="assignation_state"))   
    public TaskAssignationState getTaskAssignationState() {
        return taskAssignationState;
    }

    public void setTaskAssignationState(TaskAssignationState taskAssignationState) {
        this.taskAssignationState = taskAssignationState;
    }

我也尝试了@MapKeyColumn@MapKeyJoinColumn / @MapKeyColumn,但使用相同的版本。例外

  

引起:org.hibernate.AnnotationException:非法尝试将非集合映射为@OneToMany@ManyToMany@CollectionOfElementscom.path.taskAssignationState

我在另一个威胁中读到了类似的问题,解决方法是创建一个Set,然后加入表格。但我想有一种方法可以使用这些注释。

修改

以前的问题已修复,只需要返回一个List。

@ManyToMany 
    @JoinTable(name = "user_state_task_assignation", 
            joinColumns=@JoinColumn(name="assignation_state"),
            inverseJoinColumns=@JoinColumn(name="username"))    
    @MapKeyJoinColumn(name="state")
    public Map<User, Boolean> getUsersState() {
        return usersState;
    }
    public void setUsersState(Map<User, Boolean> usersState) {
        this.usersState = usersState;
    }

    @ManyToMany(
            cascade={CascadeType.ALL},  
            mappedBy = "taskAssignationState",
            fetch=FetchType.LAZY
    )
    public List<TaskHistory> getTasksHistory() {
        return tasksHistory;
    }
    public void setTasksHistory(List<TaskHistory> tasksHistory) {
        this.tasksHistory = tasksHistory;
    }

但现在我正面临着另一个问题:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext-dao.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.path.TaskAssignationState.usersState[java.lang.Boolean]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1412)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:546)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:872)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:202)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:251)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:248)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

1 个答案:

答案 0 :(得分:2)

尝试从getTaskAssignationState方法返回TaskAssignationState列表