JPA,可嵌入类

时间:2014-01-30 11:58:04

标签: java-ee jpa

我想为实体类添加嵌入式关系。

在我的数据库中,我有表protein

+-------------------+------------------+------+-----+---------+----------------+

| Field             | Type             | Null | Key | Default | Extra          |

+-------------------+------------------+------+-----+---------+----------------+

| PID               | int(11) unsigned | NO   | PRI | NULL    | auto_increment |

| uniprot_UniprotAC | char(6)          | YES  | MUL | NULL    |                |

| comment           | int(11) unsigned | YES  | MUL | NULL    |                |

+-------------------+------------------+------+-----+---------+----------------+

和表格benchmark

+-----------------+--------------------------------+------+-----+---------+-------+
| Field           | Type                           | Null | Key | Default | Extra |
+-----------------+--------------------------------+------+-----+---------+-------+
| ba_type         | varchar(255)                   | YES  |     | NULL    |       |

| target          | int(11) unsigned               | NO   | MUL | NULL    |       |

| rec_diluation   | varchar(255)                   | YES  |     | NULL    |       |

| comment         | text                           | YES  |     | NULL    |       |
+-----------------+--------------------------------+------+-----+---------+-------+

benchmark中,字段目标是protein

的外键

我已经从蛋白质表中找到了一个名为SupProtein的实体,效果很好。

现在我想为Benchmark类添加嵌入式关系。

这是我的基准课程:

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable
public class Benchmark implements Serializable {
    @Column(name="ba_type")
    private String type;
    @Column(name="comment")
    private String comment;
    @Column(name="recDilution")
    private String recDilution;  

    public Benchmark(){

    }

        public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getRecDilution() {
        return recDilution;
    }

    public void setRecDilution(String recDilution) {
        this.recDilution = recDilution;
    }

}

在我的实体类SupProtein中,我添加:

@Embedded
private Benchmark benchmark; 

但是当我尝试构建时,我收到以下错误:

Internal Exception: Exception [EclipseLink-7246] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The Entity class [class SupEntity.SupProtein] has an embedded attribute [benchmark] of type [class Benchmark] which is NOT an Embeddable class. Probable reason: missing @Embeddable or missing <embeddable> in orm.xml if metadata-complete = true

我没有创建任何ORM.xml文件,也无法在任何地方找到它。我是否需要orm.xml文件来创建可嵌入的关系或可能是我的问题?

1 个答案:

答案 0 :(得分:0)

  

现在我想为Benchmark类添加嵌入式关系。

我认为你有错误的概念:

  

@Embedded注释用于指定持久字段或   值为可嵌入实例的实体的属性   类。默认情况下,@ Embeddable中指定的列定义   class适用于拥有实体的表

嵌入式类不是实体,也不在表中。 如果基准是表,则必须在类上使用@Entity注释。 仅当可以将类的某些属性视为对象时才使用embeddable。

我是否需要orm.xml文件来创建可嵌入的关系或可能是我的问题? 不,orm.xml不是必需的,不是为了这个目的。

如果你想在课程之间建立关系,你应该使用@OneToOne,@ OneToMany取决于你们关系的基数

相关问题