有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JPA/eclipselink 2.6.0/mysql:生成的表名不正确

我创建了两个表:

@Entity
@Table(name="instanceOfClass")
public class InstanceOfClass {
(...)
    @OneToMany(fetch=FetchType.LAZY)
    public List<InstanceOfDataProperty> getDataProperties() {
        return dataProperties;
    }
(...)
}

@Entity
@Table(name="instanceOfDataProperty")
public class InstanceOfDataProperty  {
    (...)
    @ManyToOne(optional=false,fetch=FetchType.LAZY) 
    @JoinColumn(referencedColumnName="instance_id", nullable=false, updatable=false)
    public InstanceOfClass getInstance()
        {
        return this.instance;
        }
    (...)
 }

调用getDataProperties()时,我在日志中看到以下错误

Call: SELECT t1.id, t1.smallContent, t1.VALUE, t1.INSTANCE_id,t1.prop_id, t1.LARGECONTENT_id FROM instanceOfClass_instanceOfDataProperty t0, instanceOfDataProperty t1 WHERE ((t0.InstanceOfClass_id = ?) AND (t1.id = t0.dataProperties_id)) bind => [1 parameter bound]
Query: ReadAllQuery(name="file:/path/to/WEB-INF/classes/_jdbc/mydao" referenceClass=InstanceOfDataProperty sql="SELECT t1.id, t1.smallContent, t1.VALUE, t1.INSTANCE_id, t1.prop_id, t1.LARGECONTENT_id FROM instanceOfClass_instanceOfDataProperty t0, instanceOfDataProperty t1 WHERE ((t0.InstanceOfClass_id = ?) AND (t1.id = t0.dataProperties_id))")

(……) 原因:com。mysql。jdbc。例外。jdbc4。MySQLSyntaxErrorException:Table'user_me。instanceOfClass_instanceOfDataProperty'不存在

这个用户在哪里告诉我。instanceOfClass_InstanceOfData属性来自哪里?我该怎么解决这个问题


共 (1) 个答案

  1. # 1 楼答案

    我认为您必须在InstanceOfClass实体中使用mappedBy值作为@OneToMany注释,如下所示:

    @Entity
    @Table(name="instanceOfClass")
    public class InstanceOfClass {
    (...)
        @OneToMany(fetch=FetchType.LAZY, mappedBy="instanceOfClass")
        public List<InstanceOfDataProperty> getDataProperties() {
            return dataProperties;
        }
    (...)
    }
    

    其中instanceOfClass应该是InstanceOfDataProperty类中的getter的名称

    摘自JavaEE7 pdf,第37.1.5.1章:

    Bidirectional relationships must follow these rules.

    The inverse side of a bidirectional relationship must refer to its owning side by using the mappedBy element of the @OneToOne, @OneToMany, or @ManyToMany annotation.

    The mappedBy element designates the property or field in the entity that is the owner of the relationship. The many side of many-to-one bidirectional relationships must not define the mappedBy element. The many side is always the owning side of the relationship.

    我用实体的人和地址(1:N)做了一个快速测试。首先,我省略了mappedBy,创建了一个名为Person_Address的表。添加mappedBy之后,创建了一个表Address