-1
public class Userr extends Model{
    @Id
    public Long id;
    @OneToMany
    List<Badge> badges;
}

public class Badge extends Model{
    @Id
    public Long id;
    @ManyToOne
    Userr user;
}

这些是我的 2 个模型,这是生成的错误:

[RuntimeException : java.lang.IllegalArgumentException: 查询抛出SQLException:Column "T1.USERR_ID"not found; SQL 语句:select t0.id c0 , t1.id_ c1, t1.name_ c2, t1.desc_ c3, t1.img_url_ c4, t1.effect_ c5, t1.user_id c6 from userr t0 left outer join badge t1 on t1.userr_id = t0.id where t0.id = ? order by t0.id[42122-168] 绑定值:[ null] 查询为:(select t0.id c0 , t1.id_ c1, t1.name_ c2, t1.desc_ c3, t1.img_url_ c4, t1.effect_ c5, t1.user_id c6 from userr t0 left outer join badge t1 on t1.userr_id = t0.id where t0.id = ? order by t0.id通过引用链com.avaje.ebean.common.BeanList[0]->models.Userr["created_badges"]:)]

我想知道为什么它使用而不是 在我的模型文件中调用字段来翻译findAll查询。userr_iduser_iduser_id

我尝试更改useruserr并且它起作用了,因为它将它翻译成<classname>_id

这是一个错误吗?还是我用错了?

4

1 回答 1

1

这不是BUG!这是 DDL 生成模式的默认行为。我将向您解释常见的可能情况:

1st case:您之前没有在数据库上创建表

当您第一次构建应用程序时不使用现有表,我认为您之前遇到的问题不会发生(只要您使用默认配置值并将应用程序设置为 DEV 模式)。直截了当,假设您有 2 个像以前一样的模型:

@Entity
public class Userr extends Model {
    @Id
    public Long id;
    public String realName;
    @OneToMany
    List<Badge> badges;
}

@Entity
public class Badge extends Model {
    @Id
    public Long id;
    @ManyToOne
    Userr user;
}

在您运行应用程序时,简单的框架应该告诉 DDL 生成模式查看您连接的数据库并查找是否存在名为userrand的表badge。因为您之前没有创建过这两个表,所以框架会在您的数据库上自动生成这两个表。根据您的模型,框架将使用类似于 (PostgreSQL script) 的脚本生成您的表:

-- the java class name is 'Userr' generated as 'userr'
CREATE TABLE userr {
    -- long on java generated as bigint
    id        bigint,
    -- 'realName' on java generated as 'realname' and string on java generated as varchar whose length 255 
    realname  character varying(255),
    -- one to many 'badges' field is not generated
    CONSTRAINT pk_userr PRIMARY KEY (id), -- id as primary key
}
-- the java class name is 'Badge' generated as 'badge'
CREATE TABLE badge {
    id        bigint,
    -- 'Userr' Object with MayToOne relation generated as the foreign key to the id on 'userr' table
    userr_id  bigint,
    CONSTRAINT pk_badge PRIMARY KEY (id),                      
    CONSTRAINT fk_badge_userr FOREIGN KEY (userr_id)
       REFERENCES userr (id) MATCH SIMPLE
       ON UPDATE NO ACTION ON DELETE NO ACTION,

}

2nd case:您在数据库上有现有表

假设您之前有 2 个名称为user_account和的表badge。该user_account表如下所示:

CREATE TABLE user_account {
    id        bigint,
    real_name character varying(100),
    CONSTRAINT pk_userr PRIMARY KEY (id),
}
CREATE TABLE badge {
    id        bigint,
    user_id   bigint,
    CONSTRAINT pk_badge PRIMARY KEY (id),                      
    CONSTRAINT fk_badge_user_account FOREIGN KEY (user_id)
       REFERENCES user_account (id) MATCH SIMPLE
       ON UPDATE NO ACTION ON DELETE NO ACTION,
}

您可以使用以下代码正确使用以前的模型经过一些修改)来处理当前表:

@javax.persistence.Entity
// define this model as table named 'user_account'
@javax.persistence.Table(name = "user_account")
public class Userr extends Model {
    @Id
    public Long id;
    // define this field as column named 'real_name' whose length is 100
    @javax.persistence.Column(name = "real_name", length = 100)
    public String realName;
    @OneToMany
    List<Badge> badges;
}

@javax.persistence.Entity
public class Badge extends Model {
    @Id
    public Long id;
    @ManyToOne
    // define this field as column named 'user_id' and 
    // this act as reference to model 'Userr' using field named 'id' on it
    @javax.persistence.JoinColumn(name = "user_id", referencedColumnName = "id")
    Userr user;
}

希望这可以帮助您更好地理解 DDL 生成模式.. :)

于 2013-04-26T00:29:52.517 回答