简体   繁体   中英

Hibernate storing maps using @MapKey annotation

Following these examples , because @MapKey and @MapKeyManyToMany in hibernate is deprecated, I tryed to figure out this case, with no sucess:

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;
    }

User

@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;
    }

I have also tried @MapKeyColumn , @MapKeyJoinColumn / @MapKeyColumn instead, but with the same resault. The exception

Caused by: org.hibernate.AnnotationException : Illegal attempt to map a non collection as a @OneToMany , @ManyToMany or @CollectionOfElements : com.path.taskAssignationState

I read in another threat a similar problem, and the solution was to make a Set and then join the tables. But I guess there is a way using these annotations.

EDIT

The previous problem is fixed, just need to return an 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;
    }

But now I am facing this other probelm:

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)

尝试从getTaskAssignationState方法返回TaskAssignationState列表

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM