#919 - io.ebean package initial

This commit is contained in:
Rob Bygrave
2016-12-11 23:23:22 +13:00
parent 5821dc96b9
commit 971e2dc91b
2134 changed files with 10721 additions and 8652 deletions
@@ -0,0 +1,35 @@
package org.tests.compositekeys;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import org.tests.model.basic.CKeyParent;
import org.tests.model.basic.CKeyParentId;
import org.junit.Assert;
import org.junit.Test;
public class TestCKeyDelete extends BaseTestCase {
@Test
public void test() {
CKeyParentId id = new CKeyParentId(100, "deleteMe");
CKeyParentId searchId = new CKeyParentId(100, "deleteMe");
CKeyParent p = new CKeyParent();
p.setId(id);
p.setName("testDelete");
Ebean.save(p);
CKeyParent found = Ebean.find(CKeyParent.class).where().idEq(searchId).findUnique();
Assert.assertNotNull(found);
Ebean.delete(CKeyParent.class, searchId);
CKeyParent notFound = Ebean.find(CKeyParent.class).where().idEq(searchId).findUnique();
Assert.assertNull(notFound);
}
}
@@ -0,0 +1,80 @@
package org.tests.compositekeys;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.EbeanServer;
import io.ebean.Query;
import org.tests.model.composite.RCustomer;
import org.tests.model.composite.RCustomerKey;
import org.tests.model.composite.ROrder;
import org.tests.model.composite.ROrderPK;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class TestCKeyIdInExpression extends BaseTestCase {
@Test
public void testDummy() {
}
//@Test
public void notRanAuto_doInsert() {
RCustomerKey customerKey = new RCustomerKey("compa", "coa");
RCustomer rCustomer = new RCustomer();
rCustomer.setKey(customerKey);
rCustomer.setDescription("some foo for ms sql server");
Ebean.save(rCustomer);
ROrderPK k0 = new ROrderPK("compa", 100);
ROrder rOrder = new ROrder();
rOrder.setCustomer(rCustomer);
rOrder.setOrderPK(k0);
rOrder.setItem("Chair");
Ebean.save(rOrder);
}
//@Test
public void notRanAutomatically() {
//EbeanServer server = CreateIdExpandedFormServer.create();
EbeanServer server = Ebean.getServer(null);
ROrderPK k0 = new ROrderPK("compa", 100);
ROrderPK k1 = new ROrderPK("compa", 101);
ROrderPK k2 = new ROrderPK("b", 105);
ROrderPK k3 = new ROrderPK("c", 106);
List<ROrderPK> keys = new ArrayList<>();
keys.add(k0);
keys.add(k1);
keys.add(k2);
keys.add(k3);
Query<ROrder> query = server.find(ROrder.class).where().idIn(keys).query();
query.findList();
String sql = query.getGeneratedSql();
Assert.assertTrue(sql.contains("(t0.company=? and t0.order_number=?) or"));
Query<ROrder> query2 = server.find(ROrder.class).setId(k0);
query2.findUnique();
sql = query2.getGeneratedSql();
Assert.assertTrue(sql.contains("t0.company = ? "));
Assert.assertTrue(sql.contains(" and t0.order_number = ?"));
server.delete(ROrder.class, k0);
server.delete(ROrder.class, keys);
}
}
@@ -0,0 +1,96 @@
package org.tests.compositekeys;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebean.PagedList;
import io.ebean.Query;
import org.tests.model.basic.CKeyAssoc;
import org.tests.model.basic.CKeyDetail;
import org.tests.model.basic.CKeyParent;
import org.tests.model.basic.CKeyParentId;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class TestCKeyLazyLoad extends BaseTestCase {
@Test
public void test() {
Ebean.deleteAll(Ebean.find(CKeyDetail.class).findList());
Ebean.deleteAll(Ebean.find(CKeyParent.class).findList());
Ebean.deleteAll(Ebean.find(CKeyAssoc.class).findList());
CKeyParentId id = new CKeyParentId(1, "one");
CKeyAssoc assoc = new CKeyAssoc();
assoc.setAssocOne("assocOne");
CKeyParent p = new CKeyParent();
p.setId(id);
p.setName("testone");
p.setAssoc(assoc);
p.add(new CKeyDetail("somethine one"));
p.add(new CKeyDetail("somethine two"));
Ebean.save(p);
CKeyAssoc assoc2 = new CKeyAssoc();
assoc2.setAssocOne("assocTwo");
CKeyParentId id2 = new CKeyParentId(2, "two");
CKeyParent p2 = new CKeyParent();
p2.setId(id2);
p2.setName("testone");
p2.setAssoc(assoc2);
p2.add(new CKeyDetail("somethine one"));
p2.add(new CKeyDetail("somethine two"));
Ebean.save(p2);
exerciseMaxRowsQuery_with_embeddedId();
CKeyParentId searchId = new CKeyParentId(1, "one");
CKeyParent found = Ebean.find(CKeyParent.class).where().idEq(searchId).findUnique();
Assert.assertNotNull(found);
Assert.assertEquals(2, found.getDetails().size());
List<CKeyParent> list = Ebean.find(CKeyParent.class).findList();
Assert.assertTrue(list.size() > 1);
CKeyParent foundFirst = list.get(0);
List<CKeyDetail> details = foundFirst.getDetails();
int size = details.size();
Assert.assertTrue(size > 0);
List<Object> idList = new ArrayList<>();
idList.add(id);
idList.add(id2);
Query<CKeyParent> queryIdIn = Ebean.find(CKeyParent.class).where().idIn(idList).query();
List<CKeyParent> idInTestList = queryIdIn.findList();
Assert.assertTrue(idInTestList.size() == 2);
}
/**
* Exercise paging/maxRows type query with EmbeddedId.
*/
private void exerciseMaxRowsQuery_with_embeddedId() {
PagedList<CKeyParent> siteUserPage = Ebean.find(CKeyParent.class).where()
.orderBy("name asc")
.setMaxRows(10)
.findPagedList();
siteUserPage.getList();
}
}
@@ -0,0 +1,27 @@
package org.tests.compositekeys;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import org.tests.compositekeys.db.CaoBean;
import org.tests.compositekeys.db.CaoKey;
import org.junit.Test;
public class TestCaoCompositeKeyWithAnnotationOverrides extends BaseTestCase {
@Test
public void test() {
Ebean.deleteAll(Ebean.find(CaoBean.class).findList());
CaoKey key = new CaoKey();
key.setCustomer(123);
key.setType(1);
CaoBean bean = new CaoBean();
bean.setKey(key);
bean.setDescription("some desc");
Ebean.save(bean);
}
}
@@ -0,0 +1,168 @@
package org.tests.compositekeys;
import io.ebean.Ebean;
import io.ebean.Query;
import io.ebean.Transaction;
import org.tests.compositekeys.db.Item;
import org.tests.compositekeys.db.ItemKey;
import org.tests.compositekeys.db.Region;
import org.tests.compositekeys.db.RegionKey;
import org.tests.compositekeys.db.SubType;
import org.tests.compositekeys.db.SubTypeKey;
import org.tests.compositekeys.db.Type;
import org.tests.compositekeys.db.TypeKey;
import org.tests.lib.EbeanTestCase;
import java.util.List;
/**
* Test some of the Avaje core functionality in conjunction with composite keys like
* <ul>
* <li>write</li>
* <li>find</li>
* </ul>
*/
public class TestCore extends EbeanTestCase {
@Override
public void setUp() throws Exception {
if (isMsSqlServer()) return;
Ebean.createUpdate(Item.class, "delete from Item").execute();
Ebean.createUpdate(Region.class, "delete from Region").execute();
Ebean.createUpdate(Type.class, "delete from Type").execute();
Ebean.createUpdate(SubType.class, "delete from SubType").execute();
Transaction tx = getServer().beginTransaction();
SubType subType = new SubType();
SubTypeKey subTypeKey = new SubTypeKey();
subTypeKey.setSubTypeId(1);
subType.setKey(subTypeKey);
subType.setDescription("ANY SUBTYPE");
getServer().save(subType);
Type type = new Type();
TypeKey typeKey = new TypeKey();
typeKey.setCustomer(1);
typeKey.setType(10);
type.setKey(typeKey);
type.setDescription("Type Old-Item - Customer 1");
type.setSubType(subType);
getServer().save(type);
type = new Type();
typeKey = new TypeKey();
typeKey.setCustomer(2);
typeKey.setType(10);
type.setKey(typeKey);
type.setDescription("Type Old-Item - Customer 2");
type.setSubType(subType);
getServer().save(type);
Region region = new Region();
RegionKey regionKey = new RegionKey();
regionKey.setCustomer(1);
regionKey.setType(500);
region.setKey(regionKey);
region.setDescription("Region West - Customer 1");
getServer().save(region);
region = new Region();
regionKey = new RegionKey();
regionKey.setCustomer(2);
regionKey.setType(500);
region.setKey(regionKey);
region.setDescription("Region West - Customer 2");
getServer().save(region);
Item item = new Item();
ItemKey itemKey = new ItemKey();
itemKey.setCustomer(1);
itemKey.setItemNumber("ITEM1");
item.setKey(itemKey);
item.setUnits("P");
item.setDescription("Fancy Car - Customer 1");
item.setRegion(500);
item.setType(10);
getServer().save(item);
item = new Item();
itemKey = new ItemKey();
itemKey.setCustomer(2);
itemKey.setItemNumber("ITEM1");
item.setKey(itemKey);
item.setUnits("P");
item.setDescription("Another Fancy Car - Customer 2");
item.setRegion(500);
item.setType(10);
getServer().save(item);
tx.commit();
}
public void testFind() {
if (isMsSqlServer()) return;
List<Item> items = getServer().find(Item.class).findList();
assertNotNull(items);
assertEquals(2, items.size());
Query<Item> qItems = getServer().find(Item.class);
// qItems.where(Expr.eq("key.customer", Integer.valueOf(1)));
// I want to discourage the direct use of Expr
qItems.where().eq("key.customer", 1);
items = qItems.findList();
assertNotNull(items);
assertEquals(1, items.size());
}
/**
* This partially loads the item and then lazy loads the ManyToOne assoc
*/
public void testDoubleLazyLoad() {
if (isMsSqlServer()) return;
ItemKey itemKey = new ItemKey();
itemKey.setCustomer(2);
itemKey.setItemNumber("ITEM1");
Item item = getServer().find(Item.class).select("description").where().idEq(itemKey).findUnique();
assertNotNull(item);
assertNotNull(item.getUnits());
assertEquals("P", item.getUnits());
Type type = item.getEType();
assertNotNull(type);
assertNotNull(type.getDescription());
SubType subType = type.getSubType();
assertNotNull(subType);
assertNotNull(subType.getDescription());
}
public void testEmbeddedWithOrder() {
if (isMsSqlServer()) return;
List<Item> items = getServer().find(Item.class).order("auditInfo.created asc, type asc").findList();
assertNotNull(items);
assertEquals(2, items.size());
}
public void testFindAndOrderByEType() {
if (isMsSqlServer()) return;
List<Item> items = getServer().find(Item.class).order("eType").findList();
assertNotNull(items);
assertEquals(2, items.size());
}
}
@@ -0,0 +1,250 @@
package org.tests.compositekeys;
import io.ebean.BaseTestCase;
import io.ebean.Ebean;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import org.junit.Before;
import org.junit.Test;
import javax.persistence.CascadeType;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertEquals;
/**
* Read more at https://groups.google.com/forum/#!topic/ebean/VD1MV2-LrOc
*/
public class TestOnCascadeDeleteChildrenWithCompositeKeys extends BaseTestCase {
@Before
public void before() {
if (isMsSqlServer()) return;
// remove all the User records first
Ebean.deleteAll(Ebean.find(User.class).findList());
// insert 2 User records
Ebean.save(new User(1L));
Ebean.save(new User(2L));
}
/**
* work fine when delete User one by one
*/
@Test
public void testDeleteById() {
if (isMsSqlServer()) return;
assertEquals(2, Ebean.find(User.class).findList().size());
Ebean.delete(User.class, 1L);
Ebean.delete(User.class, 2L);
assertEquals(0, Ebean.find(User.class).findList().size());
}
/**
* Wrong SQL is generated when deleting user by list of ID
* SQL generated: delete from user_role where (user_id) in ((?,?),(?,?))
*/
@Test
public void testDeleteByIdList() {
if (isMsSqlServer()) return;
assertEquals(2, Ebean.find(User.class).findList().size());
List<Long> ids = new ArrayList<>();
ids.add(1L);
ids.add(2L);
Ebean.deleteAll(User.class, ids); // PersistenceException would be thrown here
assertEquals(0, Ebean.find(User.class).findList().size());
}
@Test
public void testFindByParentIdList() {
if (isMsSqlServer()) return;
assertEquals(2, Ebean.find(User.class).findList().size());
SpiEbeanServer spiServer = (SpiEbeanServer) Ebean.getServer(null);
BeanDescriptor<TestOnCascadeDeleteChildrenWithCompositeKeys.User> beanDescriptor =
spiServer.getBeanDescriptor(TestOnCascadeDeleteChildrenWithCompositeKeys.User.class);
BeanPropertyAssocMany<?> beanProperty = (BeanPropertyAssocMany<?>) beanDescriptor.getBeanProperty("userRoles");
List<Object> ids = new ArrayList<>();
ids.add(1L);
ids.add(2L);
beanProperty.findIdsByParentId(null, ids, null, null);
beanProperty.findIdsByParentId(1L, null, null, null);
}
@Entity
@Table(name = "em_user")
public static class User {
private Long id;
private String name;
private Set<UserRole> userRoles;
public User() {
}
public User(Long id) {
this.id = id;
}
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(cascade = CascadeType.REMOVE)
public Set<UserRole> getUserRoles() {
return userRoles;
}
public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}
}
@Entity
@Table(name = "em_user_role")
public static class UserRole implements Serializable {
private static final long serialVersionUID = 1L;
private UserRolePK pk;
private User user;
private Role role;
@EmbeddedId
public UserRolePK getPk() {
return pk;
}
public void setPk(UserRolePK pk) {
this.pk = pk;
}
@ManyToOne
@JoinColumn(name = "user_id", nullable = false, insertable = false, updatable = false)
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@ManyToOne
@JoinColumn(name = "role_id", nullable = false, insertable = false, updatable = false)
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
@Embeddable
public static class UserRolePK implements Serializable {
private static final long serialVersionUID = 1L;
private Long userId;
private Long roleId;
@Override
public int hashCode() {
return userId == null || roleId == null ? 0 : (int) (userId + roleId);
}
@Override
public boolean equals(Object other) {
if (userId == null || roleId == null) return false;
if (other instanceof UserRolePK) {
UserRolePK otherPk = (UserRolePK) other;
return userId.equals(otherPk.userId) && roleId.equals(otherPk.roleId);
}
return false;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Long getRoleId() {
return roleId;
}
public void setRoleId(Long roleId) {
this.roleId = roleId;
}
}
}
@Entity
@Table(name = "em_role")
public static class Role {
private Long id;
private String name;
private Set<UserRole> userRoles;
@Id
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(cascade = CascadeType.REMOVE)
public Set<UserRole> getUserRoles() {
return userRoles;
}
public void setUserRoles(Set<UserRole> userRoles) {
this.userRoles = userRoles;
}
}
}
@@ -0,0 +1,49 @@
package org.tests.compositekeys.db;
import javax.persistence.Embeddable;
import java.util.Date;
@Embeddable
public class AuditInfo {
private Date lastUpdated;
private Date created;
private String updatedBy;
private String createdBy;
public AuditInfo() {
created = new Date();
createdBy = "dummy";
}
public Date getLastUpdated() {
return lastUpdated;
}
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
}
@@ -0,0 +1,46 @@
package org.tests.compositekeys.db;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
@Entity
public class CaoBean {
@Id
@AttributeOverride(name = "customer", column = @Column(name = "x_cust_id"))
@AttributeOverride(name = "type", column = @Column(name = "x_type_id"))
private CaoKey key;
private String description;
@Version
private Long version;
public CaoKey getKey() {
return key;
}
public void setKey(CaoKey key) {
this.key = key;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
@@ -0,0 +1,43 @@
package org.tests.compositekeys.db;
import javax.persistence.Embeddable;
@Embeddable
public class CaoKey {
private int customer;
private int type;
public int getCustomer() {
return customer;
}
public void setCustomer(int customer) {
this.customer = customer;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int hashCode() {
int hc = 31 * 7 + customer;
hc = 31 * hc + type;
return hc;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof CaoKey) {
CaoKey k = (CaoKey) o;
return k.customer == customer && k.type == type;
}
return false;
}
}
@@ -0,0 +1,112 @@
package org.tests.compositekeys.db;
import javax.persistence.AttributeOverride;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Version;
@Entity
public class Item {
@Id
private ItemKey key;
private String description;
private String units;
private int type;
private int region;
@Embedded
@AttributeOverride(name = "lastUpdated", column = @Column(name = "DATE_MODIFIED"))
@AttributeOverride(name = "created", column = @Column(name = "DATE_CREATED"))
@AttributeOverride(name = "updatedBy", column = @Column(name = "MODIFIED_BY"))
@AttributeOverride(name = "createdBy", column = @Column(name = "CREATED_BY"))
private AuditInfo auditInfo = new AuditInfo();
@Version
private Long version;
@ManyToOne
@JoinColumn(name = "customer", referencedColumnName = "customer", insertable = false, updatable = false)
@JoinColumn(name = "type", referencedColumnName = "type", insertable = false, updatable = false)
private Type eType;
@ManyToOne
@JoinColumn(name = "customer", referencedColumnName = "customer", insertable = false, updatable = false)
@JoinColumn(name = "region", referencedColumnName = "type", insertable = false, updatable = false)
private Region eRegion;
public ItemKey getKey() {
return key;
}
public void setKey(ItemKey key) {
this.key = key;
}
public String getUnits() {
return units;
}
public void setUnits(String units) {
this.units = units;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int getRegion() {
return region;
}
public void setRegion(int region) {
this.region = region;
}
public Long getVersion() {
return version;
}
public Type getEType() {
return eType;
}
public Region getERegion() {
return eRegion;
}
public void setVersion(Long version) {
this.version = version;
}
public void setEType(Type eType) {
this.eType = eType;
}
public void setERegion(Region eRegion) {
this.eRegion = eRegion;
}
public AuditInfo getAuditInfo() {
return auditInfo;
}
}
@@ -0,0 +1,56 @@
package org.tests.compositekeys.db;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class ItemKey {
private int customer;
@Column(name = "itemNumber")
private String itemNumber;
public int getCustomer() {
return customer;
}
public void setCustomer(int customer) {
this.customer = customer;
}
public String getItemNumber() {
return itemNumber;
}
public void setItemNumber(String itemNumber) {
this.itemNumber = itemNumber;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof ItemKey)) {
return false;
}
ItemKey itemKey = (ItemKey) o;
if (customer != itemKey.customer) {
return false;
}
if (!itemNumber.equals(itemKey.itemNumber)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int result = customer;
result = 31 * result + itemNumber.hashCode();
return result;
}
}
@@ -0,0 +1,30 @@
package org.tests.compositekeys.db;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Parcel {
@Id
@Column(name = "parcelId")
private Long parcelId;
private String description;
public Long getParcelId() {
return parcelId;
}
public void setParcelId(Long parcelId) {
this.parcelId = parcelId;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
@@ -0,0 +1,45 @@
package org.tests.compositekeys.db;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class ParcelLocation {
@Id
@Column(name = "parcelLocId")
private Long parcelLocId;
private String location;
@OneToOne
@JoinColumn(name = "parcelId", referencedColumnName = "parcelId")
private Parcel parcel;
public Long getParcelLocId() {
return parcelLocId;
}
public void setParcelLocId(Long parcelLocId) {
this.parcelLocId = parcelLocId;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Parcel getParcel() {
return parcel;
}
public void setParcel(Parcel parcel) {
this.parcel = parcel;
}
}
@@ -0,0 +1,55 @@
package org.tests.compositekeys.db;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.OneToMany;
import javax.persistence.Version;
import java.util.List;
@Entity
public class Region {
@Id
private RegionKey key;
private String description;
@Version
private Long version;
@OneToMany
@JoinColumns({
@JoinColumn(name = "customer", referencedColumnName = "customer", insertable = false, updatable = false),
@JoinColumn(name = "region", referencedColumnName = "type", insertable = false, updatable = false)
})
private List<Item> items;
public RegionKey getKey() {
return key;
}
public void setKey(RegionKey key) {
this.key = key;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public List<Item> getItems() {
return items;
}
}
@@ -0,0 +1,43 @@
package org.tests.compositekeys.db;
import javax.persistence.Embeddable;
@Embeddable
public class RegionKey {
private int customer;
private int type;
public int getCustomer() {
return customer;
}
public void setCustomer(int customer) {
this.customer = customer;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int hashCode() {
int hc = 31 * 7 + customer;
hc = 31 * hc + type;
return hc;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof RegionKey) {
RegionKey k = (RegionKey) o;
return k.customer == customer && k.type == type;
}
return false;
}
}
@@ -0,0 +1,40 @@
package org.tests.compositekeys.db;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Version;
@Entity
public class SubType {
@EmbeddedId
private SubTypeKey key;
private String description;
@Version
private Long version;
public SubTypeKey getKey() {
return key;
}
public void setKey(SubTypeKey key) {
this.key = key;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
}
@@ -0,0 +1,31 @@
package org.tests.compositekeys.db;
import javax.persistence.Embeddable;
@Embeddable
public class SubTypeKey {
private int subTypeId;
public int getSubTypeId() {
return subTypeId;
}
public void setSubTypeId(int subTypeId) {
this.subTypeId = subTypeId;
}
public int hashCode() {
return 31 * 7 + subTypeId;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof SubTypeKey)) {
return false;
}
return subTypeId == ((SubTypeKey) o).subTypeId;
}
}
@@ -0,0 +1,67 @@
package org.tests.compositekeys.db;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Version;
import java.util.List;
@Entity
public class Type {
@Id
private TypeKey key;
private String description;
@Version
private Long version;
@OneToMany
@JoinColumns({
@JoinColumn(name = "customer", referencedColumnName = "customer", insertable = false, updatable = false),
@JoinColumn(name = "type", referencedColumnName = "type", insertable = false, updatable = false)
})
private List<Item> items;
@ManyToOne
private SubType subType;
public TypeKey getKey() {
return key;
}
public void setKey(TypeKey key) {
this.key = key;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public List<Item> getItems() {
return items;
}
public SubType getSubType() {
return subType;
}
public void setSubType(SubType subType) {
this.subType = subType;
}
}
@@ -0,0 +1,43 @@
package org.tests.compositekeys.db;
import javax.persistence.Embeddable;
@Embeddable
public class TypeKey {
private int customer;
private int type;
public int getCustomer() {
return customer;
}
public void setCustomer(int customer) {
this.customer = customer;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public int hashCode() {
int hc = 31 + customer;
hc = hc * 31 + type;
return hc;
}
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof TypeKey) {
TypeKey k = (TypeKey) o;
return k.customer == customer && k.type == type;
}
return false;
}
}