gpt4 book ai didi

How to use mapping table for @OneToMany and @ManytoOne bidirectional relationship?(@OneToMany和@ManytoOne双向关系如何使用映射表?)

转载 作者:bug小助手 更新时间:2023-10-28 10:10:46 26 4
gpt4 key购买 nike



I have 2 tables - Group, Expense. My requirement is that I want to establish bidirectional relationship between Group and Expense using external mapping table. The relationship from Group to Expense is @OneToMany.
Below are my Group and Expense classes.

我有两张桌子--集团,费用。我的要求是希望使用外部映射表在集团和费用之间建立双向关系。从集团到费用的关系是@OneToMany。下面是我的集团类和费用类。


@Entity
@Table(name = "GROUP")
public class Group {
@Id
@GeneratedValue(generator = "group-generator")
@GenericGenerator(name = "group-generator",
parameters = @Parameter(name = "prefix", value = "g"),
type = SplitwiseIdGenerator.class)
@Column(name = "ID")
private String id;

// other attributes...

@OneToMany(mappedBy = "group")
private List<Expense> expenses;
}

@Entity
@Table(name = "EXPENSE")
public class Expense {
@Id
@GeneratedValue(generator = "expense-generator")
@GenericGenerator(name = "expense-generator",
parameters = @Parameter(name = "prefix", value = "e"),
type = SplitwiseIdGenerator.class)
@Column(name = "ID")
private String id;

// other attributes...

@ManyToOne
@JoinTable(name = "GROUP_EXPENSE",
joinColumns = {@JoinColumn(name = "EXPENSE_ID")},
inverseJoinColumns = {@JoinColumn(name = "GROUP_ID")})
private Group group;
}

I am auto creating tables with spring.jpa.hibernate.ddl-auto=update. The above relationship is not working as I am getting below error during Spring startup
Caused by: org.hibernate.AnnotationException: Secondary table 'group_expense' for property 'org.vip.splitwise.models.Expense' is not declared (use '@SecondaryTable' to declare the secondary table).

我使用spring.jpa.hibernate.ddl-Auto=UPDATE自动创建表。上述关系不起作用,因为我在Spring启动期间出现以下错误,原因是:org.hibernate.AnnotationException:未声明属性‘org.vip.plitwie.Models.Expense’的辅助表‘group_expect’(使用‘@Second daryTable’声明辅助表)。


These are my expected db tables.

这些是我预期的数据库表。


enter image description here


Anyone please help me on this. Thanks in advance.

任何人都请帮助我在这方面。先谢谢你了。


更多回答

check section 4.4. Mapping associations to tables from documentation all will be more clearly.

检查4.4节。将关联映射到文档中的表将更加清晰。

优秀答案推荐
更多回答

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