mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Add fix and test for BUG 408 from Eddie
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package com.avaje.tests.inheritance;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.inheritance.model.CalculationResult;
|
||||
import com.avaje.tests.inheritance.model.Configurations;
|
||||
import com.avaje.tests.inheritance.model.GroupConfiguration;
|
||||
import com.avaje.tests.inheritance.model.ProductConfiguration;
|
||||
|
||||
public class TestInheritanceJoins extends BaseTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void testAssocOne() {
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
final ProductConfiguration pc = new ProductConfiguration();
|
||||
pc.setName("PC1");
|
||||
server.save(pc);
|
||||
|
||||
final GroupConfiguration gc = new GroupConfiguration();
|
||||
gc.setName("GC1");
|
||||
server.save(gc);
|
||||
|
||||
CalculationResult r = new CalculationResult();
|
||||
final Double charge = 100.0;
|
||||
r.setCharge(charge);
|
||||
r.setProductConfiguration(pc);
|
||||
r.setGroupConfiguration(gc);
|
||||
server.save(r);
|
||||
|
||||
|
||||
Query<CalculationResult> q = server.createNamedQuery(CalculationResult.class, "loadResult");
|
||||
q.setParameter("charge", charge);
|
||||
|
||||
List<CalculationResult> results = q.findList();
|
||||
|
||||
Assert.assertTrue(!results.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAssocMany() {
|
||||
Configurations configurations = new Configurations();
|
||||
|
||||
EbeanServer server = Ebean.getServer(null);
|
||||
|
||||
server.save(configurations);
|
||||
|
||||
|
||||
final GroupConfiguration gc = new GroupConfiguration("GC1");
|
||||
configurations.add(gc);
|
||||
|
||||
|
||||
server.save(gc);
|
||||
|
||||
|
||||
Configurations configurationsQueried = server.find(Configurations.class, configurations.getId());
|
||||
|
||||
List<GroupConfiguration> groups = configurationsQueried.getGroupConfigurations();
|
||||
|
||||
Assert.assertTrue(!groups.isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.avaje.tests.inheritance.model;
|
||||
|
||||
import javax.persistence.MappedSuperclass;
|
||||
|
||||
@MappedSuperclass
|
||||
public class AbstractBaseClass {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.avaje.tests.inheritance.model;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.NamedQueries;
|
||||
import javax.persistence.NamedQuery;
|
||||
|
||||
@Entity
|
||||
@NamedQueries({
|
||||
@NamedQuery(name="loadResult",
|
||||
query="find CalculationResult " +
|
||||
"fetch productConfiguration "+
|
||||
"fetch groupConfiguration "+
|
||||
"where charge = :charge")
|
||||
})
|
||||
public class CalculationResult {
|
||||
|
||||
@Id
|
||||
@Column(name="ID")
|
||||
private Integer id;
|
||||
|
||||
private double charge;
|
||||
|
||||
@ManyToOne(cascade=CascadeType.PERSIST)
|
||||
private ProductConfiguration productConfiguration;
|
||||
|
||||
@ManyToOne(cascade=CascadeType.PERSIST)
|
||||
private GroupConfiguration groupConfiguration;
|
||||
|
||||
public double getCharge() {
|
||||
return charge;
|
||||
}
|
||||
|
||||
public void setCharge(double charge) {
|
||||
this.charge = charge;
|
||||
}
|
||||
|
||||
public ProductConfiguration getProductConfiguration() {
|
||||
return productConfiguration;
|
||||
}
|
||||
|
||||
public void setProductConfiguration(ProductConfiguration productConfiguration) {
|
||||
this.productConfiguration = productConfiguration;
|
||||
}
|
||||
|
||||
public GroupConfiguration getGroupConfiguration() {
|
||||
return groupConfiguration;
|
||||
}
|
||||
|
||||
public void setGroupConfiguration(GroupConfiguration groupConfiguration) {
|
||||
this.groupConfiguration = groupConfiguration;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.avaje.tests.inheritance.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.DiscriminatorColumn;
|
||||
import javax.persistence.DiscriminatorType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
|
||||
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING)
|
||||
public class Configuration extends AbstractBaseClass{
|
||||
@Id
|
||||
@Column(name="ID")
|
||||
private Integer id;
|
||||
|
||||
|
||||
@ManyToOne
|
||||
private Configurations configurations;
|
||||
|
||||
|
||||
public Configuration(){
|
||||
super();
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Configurations getConfigurations() {
|
||||
return configurations;
|
||||
}
|
||||
|
||||
public void setConfigurations(Configurations configurations) {
|
||||
this.configurations = configurations;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.avaje.tests.inheritance.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
@Entity
|
||||
public class Configurations {
|
||||
@Id
|
||||
@Column(name="ID")
|
||||
private Integer id;
|
||||
|
||||
@OneToMany
|
||||
private List<GroupConfiguration> groupConfigurations;
|
||||
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
|
||||
public List<GroupConfiguration> getGroupConfigurations() {
|
||||
return groupConfigurations;
|
||||
}
|
||||
|
||||
|
||||
public void setGroupConfigurations(List<GroupConfiguration> groupConfigurations) {
|
||||
this.groupConfigurations = groupConfigurations;
|
||||
}
|
||||
|
||||
public void add(GroupConfiguration groupConfiguration){
|
||||
groupConfiguration.setConfigurations(this);
|
||||
groupConfigurations.add(groupConfiguration);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.avaje.tests.inheritance.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("2")
|
||||
public class GroupConfiguration extends Configuration {
|
||||
private String groupName;
|
||||
|
||||
@OneToMany(mappedBy="groupConfiguration")
|
||||
private List<CalculationResult> results;
|
||||
|
||||
public GroupConfiguration(){
|
||||
super();
|
||||
}
|
||||
|
||||
public GroupConfiguration(String name){
|
||||
super();
|
||||
this.groupName = name;
|
||||
}
|
||||
|
||||
public String getGroupName() {
|
||||
return groupName;
|
||||
}
|
||||
|
||||
public void setGroupName(String groupName) {
|
||||
this.groupName = groupName;
|
||||
}
|
||||
|
||||
public List<CalculationResult> getResults() {
|
||||
return results;
|
||||
}
|
||||
|
||||
public void setResults(List<CalculationResult> results) {
|
||||
this.results = results;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.avaje.tests.inheritance.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
@Entity
|
||||
@DiscriminatorValue("1")
|
||||
public class ProductConfiguration extends Configuration {
|
||||
private String productName;
|
||||
|
||||
@OneToMany(mappedBy="productConfiguration")
|
||||
private List<CalculationResult> results;
|
||||
|
||||
public ProductConfiguration(){
|
||||
super();
|
||||
}
|
||||
|
||||
public String getProductName() {
|
||||
return productName;
|
||||
}
|
||||
|
||||
public void setProductName(String productName) {
|
||||
this.productName = productName;
|
||||
}
|
||||
|
||||
public List<CalculationResult> getResults() {
|
||||
return results;
|
||||
}
|
||||
|
||||
public void setResults(List<CalculationResult> results) {
|
||||
this.results = results;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user