gpt4 book ai didi

java - Hibernate 使用 @MapKey 注释存储 map

转载 作者:行者123 更新时间:2023-12-02 08:18:05 25 4
gpt4 key购买 nike

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

任务分配状态

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,但结果相同。异常

Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.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)

最佳答案

尝试从 getTaskAssignationState 方法返回 TaskAssignationState 列表

关于java - Hibernate 使用 @MapKey 注释存储 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5976407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com