#1457 - Issue with @ManyToOne with @EmbeddedId where only some columns are insertable or updatable

This commit is contained in:
rob bygrave
2018-07-18 21:29:08 +12:00
parent 2d44d06f36
commit 01541bb208
14 changed files with 318 additions and 22 deletions
@@ -0,0 +1,48 @@
package org.tests.model.composite;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
@Entity
public class CkeClient {
@EmbeddedId
private CkeClientKey clientPK;
@JoinColumns({
@JoinColumn(name = "username", referencedColumnName = "username"),
@JoinColumn(name = "cod_cpny", referencedColumnName = "cod_cpny", insertable = false, updatable = false)
})
@ManyToOne(optional = false, fetch = FetchType.LAZY)
private CkeUser user;
private String notes;
public CkeUser getUser() {
return user;
}
public void setUser(CkeUser user) {
this.user = user;
}
public CkeClientKey getClientPK() {
return clientPK;
}
public void setClientPK(CkeClientKey clientPK) {
this.clientPK = clientPK;
}
public String getNotes() {
return notes;
}
public void setNotes(String notes) {
this.notes = notes;
}
}
@@ -0,0 +1,53 @@
package org.tests.model.composite;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.Objects;
@Embeddable
public class CkeClientKey {
@Basic(optional = false)
@Column(name = "cod_cpny")
private int codCompany;
@Basic(optional = false)
@Column(name = "cod_client")
private String codClient;
public CkeClientKey(int codCompany, String codClient) {
this.codCompany = codCompany;
this.codClient = codClient;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CkeClientKey that = (CkeClientKey) o;
return codCompany == that.codCompany &&
Objects.equals(codClient, that.codClient);
}
@Override
public int hashCode() {
return Objects.hash(codCompany, codClient);
}
public int getCodCompany() {
return codCompany;
}
public void setCodCompany(int codCompany) {
this.codCompany = codCompany;
}
public String getCodClient() {
return codClient;
}
public void setCodClient(String codClient) {
this.codClient = codClient;
}
}
@@ -0,0 +1,29 @@
package org.tests.model.composite;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
@Entity
public class CkeUser {
@EmbeddedId
private CkeUserKey userPK;
private String name;
public CkeUserKey getUserPK() {
return userPK;
}
public void setUserPK(CkeUserKey userPK) {
this.userPK = userPK;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,53 @@
package org.tests.model.composite;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.Objects;
@Embeddable
public class CkeUserKey {
@Basic(optional = false)
@Column(name = "cod_cpny")
private int codCompany;
@Basic(optional = false)
@Column(name = "username")
private String username;
public CkeUserKey(int codCompany, String username) {
this.codCompany = codCompany;
this.username = username;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CkeUserKey that = (CkeUserKey) o;
return codCompany == that.codCompany &&
Objects.equals(username, that.username);
}
@Override
public int hashCode() {
return Objects.hash(codCompany, username);
}
public int getCodCompany() {
return codCompany;
}
public void setCodCompany(int codCompany) {
this.codCompany = codCompany;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
@@ -0,0 +1,50 @@
package org.tests.model.composite;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import org.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class TestCompositeKeyUserClient extends BaseTestCase {
@Test
public void test() {
CkeUser user0 = new CkeUser();
user0.setUserPK(new CkeUserKey(20, "sally"));
user0.setName("sally");
Ebean.save(user0);
CkeUser user1 = new CkeUser();
user1.setUserPK(new CkeUserKey(20, "frank"));
user1.setName("hello");
Ebean.save(user1);
CkeClient client = new CkeClient();
client.setNotes("try it");
client.setClientPK(new CkeClientKey(20, "susan"));
client.setUser(user1);
LoggedSqlCollector.start();
Ebean.save(client);
client.setNotes("update it");
client.setUser(user0);
Ebean.save(client);
List<String> sql = LoggedSqlCollector.stop();
assertThat(sql).hasSize(2);
assertThat(sql.get(0)).contains("insert into cke_client (cod_cpny, cod_client, notes, username) values (?,?,?,?)");
assertThat(sql.get(1)).contains("update cke_client set notes=?, username=? where cod_cpny=? and cod_client=?");
Ebean.delete(client);
}
}