1

我有一个奇怪的问题。我正在使用带有 ebeans (=> mysql) 的 play 2.1-SNAPSHOT。我有一个非常小的(测试)设置,由于某种原因,数据库更新和删除不起作用。项目是在数据库中创建的......但更新它们不起作用。

这是我的 bean(它扩展了一个添加时间戳(创建和修改日期)的超类):

AbstractTimestamp(超类):

@MappedSuperclass
public abstract class AbstractTimestampedBean extends AbstractIdentifiableBean {
    /** The date this item has been created. */
    @CreatedTimestamp
    public Timestamp createdTime;
}

Project Bean(删除了不重要的东西) - hashCode 和 equals 已由 eclipse 创建 - 这里我们覆盖 play.db.ebean.Model 的方法:

@Entity
@Table(name = "Projects")
public class Project extends AbstractTimestampedBean {

    private static final long serialVersionUID = -6160140283947231026L;

    @NotNull
    public String title;

    @NotNull
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public User owner;

    @NotNull
    @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public User creator;

    @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public Set<User> participants;

    @EnumMapping(nameValuePairs = "ACTIVE=A,INACTIVE=I,EXPIRED=E")
    public enum Status {
        ACTIVE, INACTIVE, EXPIRED
    }

    public Project() {
    }

    public Project(final String title, final User creator) {
        this.title = title;
        this.creator = creator;
        this.owner = creator;
    }

    /*
     * (non-Javadoc)
     * 
     * @see play.db.ebean.Model#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result
                        + (this.creator == null ? 0 : this.creator.hashCode());
        result = prime * result
                        + (this.owner == null ? 0 : this.owner.hashCode());
        result = prime * result
                        + (this.participants == null ? 0 : this.participants
                                        .hashCode());
        result = prime * result
                        + (this.title == null ? 0 : this.title.hashCode());
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see play.db.ebean.Model#equals(java.lang.Object)
     */
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (this.getClass() != obj.getClass()) {
            return false;
        }
        final Project other = (Project) obj;
        if (this.creator == null) {
            if (other.creator != null) {
                return false;
            }
        } else if (!this.creator.equals(other.creator)) {
            return false;
        }
        if (this.owner == null) {
            if (other.owner != null) {
                return false;
            }
        } else if (!this.owner.equals(other.owner)) {
            return false;
        }
        if (this.participants == null) {
            if (other.participants != null) {
                return false;
            }
        } else if (!this.participants.equals(other.participants)) {
            return false;
        }
        if (this.title == null) {
            if (other.title != null) {
                return false;
            }
        } else if (!this.title.equals(other.title)) {
            return false;
        }
        return true;
    }

这是一个非常简单的测试用例:

第一次运行创建一个项目 - 检查它是否存在(这里没有失败)

然后我们更新一些东西 - 存储它 - 并再次断言......在这里我可以看到数据库条目尚未更新。

http://pastebin.com/7zdzWGXw

这是我们在这里使用的超类:

public abstract class AbstractPersistableTestCase {
    @Transactional
    void saveBean(final Model bean) {
        Ebean.save(bean);
    }

    @Transactional
    void deleteBean(final Model bean) {
        Ebean.delete(bean);
    }

    @Transactional
    <T extends Model> void deleteBeans(final List<T> beans) {
        Ebean.delete(beans);
    }
}

来自 jUnit4 的错误消息:

这是更新案例中标题的断言=>请参阅:db条目尚未更新:

[error] Test test.models.ProjectTest.createAndUpdateProject failed: expected:<'Project_[NEW_]1350681993608'> but was:<Project_[]1350681993608'>

当我尝试删除项目时会发生这种情况:

[error] Test test.models.ProjectTest.deleteProjects failed: Data has changed. updated [0] rows sql[delete from user where id=? and name is null and email is null and created_time is null] bind[null]

你们知道为什么会这样吗?我在这里真的很沮丧...

问候,

萨沙

4

1 回答 1

0

在我看来,您没有在课程中添加 Id。

尝试将此添加到您的超类:

@MappedSuperclass
public abstract class AbstractModel extends play.db.ebean.Model
{
    @Id
    public Long id;

    public Long getId()
    {
        return id;
    }

    // ... here your other attributes
}
于 2012-10-26T11:28:21.370 回答