我正在尝试使用同一个类的One to One hibernate关系创建“一系列命令”:
@Entity
public class Command {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true)
private Long id;
@ElementCollection
@LazyCollection(LazyCollectionOption.FALSE)
private List<String> commandWithArguments = new ArrayList<String>();
private ExecutionStatus status = ExecutionStatus.NOT_EXECUTED;
@OneToOne(mappedBy = "parentCommand", cascade = CascadeType.ALL)
private Command child;
@OneToOne(mappedBy = "child", cascade = CascadeType.ALL)
private Command parentCommand;
@OneToOne(mappedBy = "command")
private ExecutionResult result;
public Command() {
}
....
hibernate是否支持这样的关系?它在初始化期间抛出以下异常:
Initial SessionFactory creation failed.org.hibernate.AnnotationException: Unknown mappedBy in: com.dockerhosting.domain.system.Command.child, referenced property unknown: com.dockerhosting.domain.system.Command.parentCommand
但是你看不到parentCommand属性。
我正在使用hibernate 4.3.8
答案 0 :(得分:1)
如果是父母或孩子,您需要指定谁将在您的双向关系(拥有方)中携带外键
子将包含parrent的参考(FK)
@OneToOne(cascade = CascadeType.ALL)
private Command child;
@OneToOne(mappedBy = "child", cascade = CascadeType.ALL)
private Command parentCommand;
父母将包含孩子的参考(FK)
@OneToOne(mappedBy = "parentCommand", cascade = CascadeType.ALL)
private Command child;
@OneToOne(cascade = CascadeType.ALL)
private Command parentCommand;
答案 1 :(得分:1)
我认为你的问题是你为同一个映射定义了两个mappedBy
属性,所以Hibernate会在映射中感到困惑。
所以使用其中两个是不必要的,你有两个选择:
1。要在班级中仅使用一个Command
对象,并将其映射为OneToOne
,如下所示:
@OneToOne(cascade = CascadeType.ALL)
private Command parentCommand;
2。或者为了使其双向使用您的类中的两个Command
对象,并使用OneToOne
映射其中两个{仅与孩子一起使用mappedBy
来引用parentCommand
:
@OneToOne(mappedBy = "parentCommand", cascade = CascadeType.ALL)
private Command child;
@OneToOne(cascade = CascadeType.ALL)
private Command parentCommand;