#1952 - Entity bean with @Id and ONLY @WhenCreated treated as reference bean - not inserted

This commit is contained in:
rob bygrave
2020-02-21 00:14:23 +13:00
parent a23d9f7db8
commit e4390ed8a2
8 changed files with 136 additions and 10 deletions
@@ -0,0 +1,19 @@
package org.tests.model.embedded;
import io.ebean.DB;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestEmbeddedEqualsWhenCreated {
@Test
public void test() {
UserInterestLive bean = new UserInterestLive(new UserInterestLiveKey(1L, 2L));
bean.save();
final UserInterestLive found = DB.find(UserInterestLive.class, bean.getKey());
assertEquals(found, bean); // <- failed as bean not inserted
}
}
@@ -0,0 +1,34 @@
package org.tests.model.embedded;
import io.ebean.Model;
import io.ebean.annotation.CreatedTimestamp;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import java.util.Date;
@Entity
public class UserInterestLive extends Model {
@EmbeddedId
private final UserInterestLiveKey key;
@CreatedTimestamp
private Date createdAt;
public UserInterestLive(UserInterestLiveKey key) {
this.key = key;
}
public UserInterestLiveKey getKey() {
return key;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
}
@@ -0,0 +1,30 @@
package org.tests.model.embedded;
import javax.persistence.Embeddable;
import java.util.Objects;
@Embeddable
public class UserInterestLiveKey {
private long userId;
private long liveId;
public UserInterestLiveKey(long userId, long liveId) {
this.userId = userId;
this.liveId = liveId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserInterestLiveKey that = (UserInterestLiveKey) o;
return userId == that.userId &&
liveId == that.liveId;
}
@Override
public int hashCode() {
return Objects.hash(userId, liveId);
}
}