gpt4 book ai didi

java - Hibernate 继承 - 没有用 Id 注释的字段

转载 作者:行者123 更新时间:2023-11-30 11:16:10 24 4
gpt4 key购买 nike

我一直在阅读有关 hibernate 继承的一些资料,但在将其应用到我的情况时遇到了困难。 I tried doing what this post mentioned which seems like it should be what I want.

这是我正在使用的 3 个类 - 每个类都应该有自己的表:

  • 父类
    • 子类
  • 分隔表

下面基本上是我列出的内容。但是,当我尝试在 Child 类服务上运行我的测试时,它们都因 java.lang.IllegalStateException: no field annotated with Id 而失败。现在,如果我正确理解那篇文章/博客文章,我就不需要用 Id 注释的 ChildClass,因为它应该在父类中获取。

我可能完全错了,因为我是 Hibernate 的新手,但如果你们有任何想法,我很乐意听到我做错了什么。

父类

@Table(name="parent_class")
@Inheritance(strategy=InheritanceType.JOINED)
public class ParentClass {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long m_id;

@Column(nullable=false, length=2500)
private String m_variable;

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="s_key", nullable=false)
private SeparateTable m_sTable;

etc...
}

子类

@Table(name="child_class")
@PrimaryKeyJoinColumn(name="m_id")
public class ChildClass extends ParentClass {

@Column(nullable=false, length=100)
private String m_name;

@Column(nullable=true, length=250)
private String m_description;

etc...
}

分隔表

@Table(name="separate_table")
public class SeparateTable {

@Id
@Column(unique=true, nullable=false, length=100)
private String m_user;

@OneToMany(mappedBy="m_sTable", cascade={CascadeType.ALL}, fetch=FetchType.LAZY, orphanRemoval=true)
private Set<ParentClass> m_parents;

@OneToMany(mappedBy="m_sTable", cascade={CascadeType.ALL}, fetch=FetchType.LAZY, orphanRemoval=true, targetEntity=ParentClass.class)
private Set<ChildClass> m_children;

etc...
}

最佳答案

m_id 的范围更改为 protected

protected Long m_id;

关于java - Hibernate 继承 - 没有用 Id 注释的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25025458/

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