我有一个奇怪的问题。我正在使用eBeans(=> mysql)的播放2.1-snapshot。我有一个很小的(测试)设置,由于某些原因,数据库更新和删除行不通。项目是在DB中创建的...但是更新它们不起作用。
这是我的bean(扩展了一个添加时间戳的超类(创建和修改日期)):
AbstractTimestamp(超类):
@MappedSuperclass
public abstract class AbstractTimestampedBean extends AbstractIdentifiableBean {
/** The date this item has been created. */
@CreatedTimestamp
public Timestamp createdTime;
}
Project Bean(删除了不重要的内容) - Eclipse创建了HashCode和等于等于 - 我们在这里覆盖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;
}
这是非常简单的测试用例:
首先运行创建一个项目 - 检查它是否存在(这里没有任何失败)
然后我们更新一些东西 - 存储它 - 再次断言...在这里我可以看到DB条目尚未更新。
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]
你们是否知道为什么会发生这种情况?我在这里真的很沮丧...
问:
sascha
在我看来您没有在类中添加ID。
尝试将其添加到您的超类:
@MappedSuperclass
public abstract class AbstractModel extends play.db.ebean.Model
{
@Id
public Long id;
public Long getId()
{
return id;
}
// ... here your other attributes
}