2

エンティティとJPAを使用してツリーを作成しようとしています。次のプロパティを持つクラスがあります。

public class Dir
{

@Id
@Basic(optional = false)
@NotNull
@Column(name = "dirId")
private Integer dirId;

@OneToOne(mappedBy="dirId", cascade= CascadeType.ALL)
private Dir parent;
...

ノードはその親が誰であるかを知っており、親がない場合はルートです。これで簡単にツリーを構築できます。しかし...私はマッピングがこの考えに正しいとは思いません。デプロイしようとすると、次のエラーが発生します。

An incompatible mapping has been encountered between [class com.dv.oa.model.entity.dir.Dir] and [class com.dv.oa.model.entity.dir.Dir]. This usually occurs when the cardinality of a mapping does not correspond with the cardinality of its backpointer.

カーディナリティについて話します。しかし、これは意味がありません。ノードは1つの親しか持つことができません。これが私が選んだ理由です@OneToOne

誰かがこれに光を当てることができますか?これを尋ねる別の方法は、エンティティをそれ自体の別のインスタンスにどのようにマップするかということだと思います。

編集

これが私のテーブル構造です:

mysql> describe dir;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| dirId        | int(11)      | NO   | PRI | NULL    |       |
| DTYPE        | varchar(31)  | YES  |     | NULL    |       |
| dirName      | varchar(255) | YES  |     | NULL    |       |
| companyOwner | int(11)      | YES  | MUL | NULL    |       |
| userOwner    | int(11)      | YES  | MUL | NULL    |       |
| parent       | int(11)      | YES  |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
4

3 に答える 3

5

マッピングの所有側の間違った列を指しています。また、1 つの親が多くの子を持つことができるため、関係は OneToOne ではありません。

@Entity
public class Dir
{

  //This field is a table column
  //It uniquely identifies a row on the DIR table
  @Id
  private int dirId;

  //This field is a table column
  // It identifies the parent of the current row
  // It it will be written as the type of dirId
  // By default this relationship will be eagerly fetched
  // , which you may or may not want
  @ManyToOne(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE})
  private Dir parent;

  //This field is not a table column
  // It is a collection of those Dir rows that have this row as a parent. 
  // This is the other side of the relationship defined by the parent field.
  @OneToMany(mappedBy="parent")
  private Set<Dir> children;
}
于 2013-01-17T21:14:51.823 に答える
2

問題はプロパティmapsByにあるようです。削除してみてください。実装にそのようなものを使用する必要はないと思います。

@Entity
public class Dir {

@Id
@Basic(optional = false)
@NotNull
@Column(name = "dirId")
private Integer dirId;

@OneToOne(cascade=CascadeType.ALL )
private Dir parent;

    ....

}
于 2013-01-17T21:06:46.113 に答える
-2

Composite Patternの実装を行う必要があります。JPAで簡単に行うことができます:

  • FileSystemItem などの抽象基本クラスを作成します。
  • Node.js を拡張する Folder という名前のクラスと File という名前のクラスを作成します。
  • 継承戦略を選択します。このために、おそらくディスクリミネーターを使用して、両方のタイプを 1 つのファイルに含めることができます。
  • 属性名を基本クラスに入れます。
  • Files ではなく FileSystemItems のフォルダー クラスでコレクションを作成します。

それだけです。もう 1 つの重要な決定事項は、add メソッドをどうするかです。基本クラスに配置すると、誰かがファイルでそれを呼び出した場合に、サポートされていない操作の例外をスローする必要があります。

于 2013-01-17T21:22:53.933 に答える