You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mi...@apache.org on 2010/05/03 18:00:33 UTC

svn commit: r940493 - in /openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity: Dependent.java DependentId.java Employee.java EmployeeId.java TestCountDistinctMultiCols.java

Author: mikedd
Date: Mon May  3 16:00:33 2010
New Revision: 940493

URL: http://svn.apache.org/viewvc?rev=940493&view=rev
Log:
OPENJPA-1483: Support count distinct compound key
> Submitted By: Heath Thomann, merged from Fay Wang's changes in trunk

Added:
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent.java   (with props)
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId.java   (with props)
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee.java   (with props)
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/EmployeeId.java   (with props)
    openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestCountDistinctMultiCols.java   (with props)

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent.java?rev=940493&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent.java Mon May  3 16:00:33 2010
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.enhance.identity;
+
+import javax.persistence.*;
+
+@Entity
+@Table(name="DEP2_MBI")
+public class Dependent {
+    @EmbeddedId
+    DependentId id;
+    
+    @ManyToOne Employee emp;
+    
+    public Employee getEmp() {
+        return emp;
+    }
+    
+    public void setEmp(Employee emp) {
+        this.emp = emp;
+    }
+    
+    public DependentId getId() {
+        return id;
+    }
+    
+    public void setId(DependentId id) {
+        this.id = id;
+    }
+    
+    public boolean equals(Object o) {
+        if (o == null) return false;
+        if (!(o instanceof Dependent)) return false;
+        Dependent d0 = (Dependent)o;
+        DependentId id0 = d0.getId();
+        if (id == null && id0 != null) return false;
+        if (!id.equals(id0)) return false;
+        Employee e0 = d0.getEmp();
+        if (emp != null && !emp.equals(e0)) return false;
+        if (emp == null && e0 != null) return false;
+        return true;
+    }
+    
+    public int hashCode() {
+        int ret = 0;
+        ret = ret * 31 + id.hashCode();
+        ret = ret * 31 + emp.hashCode();
+        return ret;
+    }
+    
+}

Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Dependent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId.java?rev=940493&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId.java Mon May  3 16:00:33 2010
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.enhance.identity;
+
+import javax.persistence.*;
+
+@Embeddable
+public class DependentId {
+    String name;
+    
+    public DependentId() {}
+    
+    public DependentId(String name, EmployeeId empPK) {
+        this.name = name;
+    }
+    
+    public void setName(String name) {
+        this.name = name;
+    }
+    
+    public String getName() {
+        return name;
+    }
+
+	@Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result + ((name == null) ? 0 : name.hashCode());
+		return result;
+	}
+
+	@Override
+	public boolean equals(Object obj) {
+		if (this == obj)
+			return true;
+		if (obj == null)
+			return false;
+		if (getClass() != obj.getClass())
+			return false;
+		DependentId other = (DependentId) obj;
+		if (name == null) {
+			if (other.name != null)
+				return false;
+		} else if (!name.equals(other.name))
+			return false;
+		return true;
+	}
+}

Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/DependentId.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee.java?rev=940493&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee.java Mon May  3 16:00:33 2010
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.enhance.identity;
+
+import javax.persistence.*;
+
+import java.util.*;
+
+@Entity
+@Table(name="EMP2_MBI")
+public class Employee {
+    @EmbeddedId
+    EmployeeId empId;
+    
+    @OneToMany(mappedBy="emp")
+    List<Dependent> dependents = new ArrayList<Dependent>();
+    
+    public EmployeeId getEmpId() {
+        return empId;
+    }
+    
+    public void setEmpId(EmployeeId empId) {
+        this.empId = empId;
+    }
+    
+    public List<Dependent> getDependents() {
+        return dependents;
+    }
+    
+    public void setDependents(List<Dependent> dependents) {
+        this.dependents = dependents;
+    }
+    
+    public void addDependent(Dependent d) {
+        dependents.add(d);
+    }
+    
+    public boolean equals(Object o) {
+        if (o == null) return false;
+        if (!(o instanceof Employee)) return false;
+        Employee e0 = (Employee)o;
+        EmployeeId eid0 = e0.getEmpId();
+        List<Dependent> ds0 = e0.getDependents();
+        if (!empId.equals(eid0)) return false;
+        if (ds0 != null && ds0.size() != 0 && dependents == null) return false; 
+        if (ds0 == null && dependents != null && dependents.size() != 0)
+            return false;
+        if (ds0 == null && dependents == null) return true;
+        if (ds0 != null && dependents != null) { 
+            if (ds0.size() != dependents.size()) return false;
+        }
+        return true;
+    }
+    
+    public int hashCode() {
+        int ret = 0;
+        ret = ret * 31 + empId.hashCode();
+        for (Dependent d : dependents) {
+            ret = ret * 31 +d.id.hashCode();
+        }
+        return ret;
+    }
+}

Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/Employee.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/EmployeeId.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/EmployeeId.java?rev=940493&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/EmployeeId.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/EmployeeId.java Mon May  3 16:00:33 2010
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.enhance.identity;
+
+import javax.persistence.*;
+
+@Embeddable
+public class EmployeeId {
+    String firstName;
+    String lastName;
+    
+    public EmployeeId() {}
+    
+    public EmployeeId(String firstName, String lastName) {
+        this.firstName = firstName;
+        this.lastName = lastName;
+    }
+    
+    public void setFirstName(String firstName) {
+        this.firstName = firstName;
+    }
+    
+    public String getFirstName() {
+        return firstName;
+    }
+    
+    public void setLastName(String lastName) {
+        this.lastName = lastName;
+    }
+    
+    public String getLastName() {
+        return lastName;
+    }
+    
+    public boolean equals(Object o) {
+        if (o == null) return false;
+        if (!(o instanceof EmployeeId)) return false;
+        EmployeeId eid = (EmployeeId)o;
+        String firstName0 = eid.getFirstName();
+        String lastName0 = eid.getLastName();
+        if (firstName != null && !firstName.equals(firstName0)) return false;
+        if (firstName == null && firstName0 != null) return false;
+        if (lastName != null && !lastName.equals(lastName0)) return false;
+        if (lastName == null && lastName0 != null) return false;
+        return true;
+    }
+    
+    public int hashCode() {
+        int ret = 0;
+        ret = ret * 31 + firstName.hashCode();
+        ret = ret * 31 + lastName.hashCode();
+        return ret;
+    }
+    
+}

Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/EmployeeId.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestCountDistinctMultiCols.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestCountDistinctMultiCols.java?rev=940493&view=auto
==============================================================================
--- openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestCountDistinctMultiCols.java (added)
+++ openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestCountDistinctMultiCols.java Mon May  3 16:00:33 2010
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.    
+ */
+package org.apache.openjpa.persistence.enhance.identity;
+
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestCountDistinctMultiCols extends SingleEMFTestCase {
+
+    public void setUp() throws Exception {
+        super.setUp(DROP_TABLES,Employee.class,EmployeeId.class,Dependent.class,DependentId.class);
+    }
+    
+    public void testCountDistinctMultiCols() {
+        EntityManager em = emf.createEntityManager(); 
+
+        Employee emp1 = new Employee();
+        EmployeeId empId1 = new EmployeeId();
+        empId1.setFirstName("James");
+        empId1.setLastName("Bond");
+        emp1.setEmpId(empId1);
+        
+        Employee emp2 = new Employee();
+        EmployeeId empId2 = new EmployeeId();
+        empId2.setFirstName("James");
+        empId2.setLastName("Obama");
+        emp2.setEmpId(empId2);
+        
+        Dependent dep1 = new Dependent();
+        DependentId depId1 = new DependentId();
+        dep1.setEmp(emp1);
+        depId1.setName("Alan");
+        dep1.setId(depId1);
+        
+        Dependent dep2 = new Dependent();
+        DependentId depId2 = new DependentId();
+        dep2.setEmp(emp2);
+        depId2.setName("Darren");
+        dep2.setId(depId2);
+        
+        em.persist(emp1);
+        em.persist(emp2);
+        em.persist(dep1);
+        em.persist(dep2);
+        
+        em.getTransaction().begin();
+        em.flush();        
+        em.getTransaction().commit();
+        
+        String[] jpqls = {
+            "SELECT COUNT (DISTINCT d2.emp) FROM Dependent d2",
+            "SELECT COUNT (DISTINCT e2.dependents) FROM Employee e2",
+            "select count (DISTINCT d2) from Dependent d2",
+        };
+        
+        for (int i = 0; i < jpqls.length; i++) {
+            Query q = em.createQuery(jpqls[i]) ;
+            Long o = (Long)q.getSingleResult();
+            int count = (int)o.longValue();
+            assertEquals(2, count);
+        }
+    }
+}

Propchange: openjpa/branches/1.3.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/enhance/identity/TestCountDistinctMultiCols.java
------------------------------------------------------------------------------
    svn:eol-style = native