Vaadin 示例项目在使用 @ManyToMany 时遇到问题

问题描述 投票:0回答:1

这是 Vaadin 示例项目遇到 @OneToMany @ManyToOne 问题的后续问题 (为了一次问一个问题)

这是我的公共示例项目,它经过简化并重现了我正在努力解决的场景:https://github.com/sketchbook22/help

我正在开发的机器使用的是java版本

java --版本 java 19.0.1 2022-10-18 Java(TM) SE 运行时环境(版本 19.0.1+10-21) Java HotSpot(TM) 64 位服务器 VM(内部版本 19.0.1+10-21,混合模式,共享) 我从 VaadinStart 创建了这个项目,然后对其进行了修改。我能够得到一个 @OneToOne 映射来工作。在我在上一个问题中收到的帮助下,@OneToMany 和 @ManyToOne 也能正常工作(耶!),我无法让双向 JPA @ManyToMany 映射正常工作。我一直在努力解决这个问题,但将其从上一个问题中分离出来。

我希望 SamplePerson 有 2 辆车,也可以有其他 SamplePerson 作为司机。 相关数据.sql

insert into car(version, id, color) values (1, 1, 'cocoa')
insert into car(version, id, color) values (1, 2, 'cream')
insert into sample_person(version, id, ape, cars, d, e) values (1, 1, 1, ARRAY [1, 2], 'd', 'e')

Car.java相关部分

@JsonBackReference
@ManyToMany(mappedBy="cars")
private Set<SamplePerson> drivers;

SamplePerson.java 的相关部分

@JsonManagedReference
@ManyToMany
@JoinTable(
        name = "CAR_DRIVER_ASSOC",
        joinColumns = {@JoinColumn(name="CAR_ID")},
        inverseJoinColumns = {@JoinColumn(name="ID")}
)
private Set<Car> cars;

输出的错误的相关部分

mvnw

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-02-06T13:03:22.325-05:00 ERROR 9620 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in com.example.application.Application: Failed to execute SQL script statement #4 of file [C:\git\help\target\classes\data.sql]: insert into sample_person(version, id, ape, cars, d, e) values (1, 1, 1, ARRAY [1, 2], 'd', 'e')
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1773) ~[spring-beans-6.1.3.jar:6.1.3]
...
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #4 of file [C:\git\help\target\classes\data.sql]: insert into sample_person(version, id, ape, cars, d, e) values (1, 1, 1, ARRAY [1, 2], 'd', 'e')
        at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:282) ~[spring-jdbc-6.1.3.jar:6.1.3]
...
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "CARS" not found; SQL statement:
insert into sample_person(version, id, ape, cars, d, e) values (1, 1, 1, ARRAY [1, 2], 'd', 'e') [42122-224]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:514) ~[h2-2.2.224.jar:2.2.224]
jpa many-to-many vaadin h2
1个回答
0
投票

@CFrick 再次感谢您在评论中推动正确的方向。

我现在明白,多对多关系可以通过关联表来完成,我在仍然使用 Vaadin 示例代码的 AbstractEntity 和内存数据库中的 h2 的同时能够弄清楚该关联表。

我在公共 GitHub 项目中做了一个标签/发布,以防它可以帮助其他人。 https://github.com/sketchbook22/help/releases/tag/after_77949994

我还有更多问题,但这些将是单独的帖子。非常感谢!

© www.soinside.com 2019 - 2024. All rights reserved.