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.
这些是我预期的数据库表。
Anyone please help me on this. Thanks in advance.
任何人都请帮助我在这方面。先谢谢你了。
更多回答
优秀答案推荐
我是一名优秀的程序员,十分优秀!