You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by cu...@apache.org on 2014/09/19 19:59:00 UTC

svn commit: r1626287 - in /openjpa/trunk: openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/

Author: curtisr7
Date: Fri Sep 19 17:59:00 2014
New Revision: 1626287

URL: http://svn.apache.org/r1626287
Log:
OPENJPA-2505 : Properly init MetaDataRepository when obtaining a reference in EntityManagerFactory.getMetaModel.

Added:
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressEntity.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressPk.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/MyUserEntity.java   (with props)
    openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/TestCriteriaInitialization.java   (with props)
Modified:
    openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressEntity.java?rev=1626287&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressEntity.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressEntity.java Fri Sep 19 17:59:00 2014
@@ -0,0 +1,61 @@
+/*
+ * 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.criteria.init;
+
+import java.io.Serializable;
+
+import javax.persistence.EmbeddedId;
+import javax.persistence.Entity;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.Table;
+
+@Entity
+@Table(name = "ADDRESSES")
+public class AddressEntity implements Serializable {
+	private static final long serialVersionUID = -6392378887188492506L;
+
+	@EmbeddedId
+    private AddressPk id;
+
+    @ManyToOne
+    @JoinColumn(name = "USERID",
+        nullable = false,
+        insertable = false,
+        updatable = false)
+    private MyUserEntity user;
+
+    public AddressEntity() {
+
+    }
+    public AddressEntity(AddressPk p) {
+        id = p;
+    }
+    public MyUserEntity getUser() {
+        return user;
+    }
+
+	public AddressPk getId() {
+		return id;
+	}
+
+	public void setId(AddressPk id) {
+		this.id = id;
+	}
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressPk.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressPk.java?rev=1626287&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressPk.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressPk.java Fri Sep 19 17:59:00 2014
@@ -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.criteria.init;
+
+import java.io.Serializable;
+
+import javax.persistence.Column;
+import javax.persistence.Embeddable;
+
+@Embeddable
+public class AddressPk implements Serializable {
+	private static final long serialVersionUID = -1108496204649786405L;
+
+	@Column(name = "ADDRESS_NAME", nullable = false, updatable = false)
+    private String addressName;
+
+    @Column(name = "USERID", nullable = false, updatable = false)
+    private Long userId;
+
+    public AddressPk(String addressName, Long userId) {
+        this.addressName = addressName;
+        this.userId = userId;
+    }
+
+    public AddressPk() {
+    }
+
+    @Override
+	public int hashCode() {
+		final int prime = 31;
+		int result = 1;
+		result = prime * result
+				+ ((addressName == null) ? 0 : addressName.hashCode());
+		result = prime * result + ((userId == null) ? 0 : userId.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;
+		AddressPk other = (AddressPk) obj;
+		if (addressName == null) {
+			if (other.addressName != null)
+				return false;
+		} else if (!addressName.equals(other.addressName))
+			return false;
+		if (userId == null) {
+			if (other.userId != null)
+				return false;
+		} else if (!userId.equals(other.userId))
+			return false;
+		return true;
+	}
+
+	public String getAddressName() {
+        return addressName;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressPk.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/MyUserEntity.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/MyUserEntity.java?rev=1626287&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/MyUserEntity.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/MyUserEntity.java Fri Sep 19 17:59:00 2014
@@ -0,0 +1,77 @@
+/*
+ * 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.criteria.init;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+import javax.persistence.Table;
+
+@Entity
+@Table(name = "MY_USERS")
+public class MyUserEntity {
+
+    @Id
+    @Column(name = "USERID", nullable = false, updatable = false)
+    private Long id;
+
+    @Column(name = "USERNAME")
+    private String username;
+
+    @OneToMany(mappedBy = "user", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
+    private List<AddressEntity> addresses = new ArrayList<AddressEntity>();
+
+    public MyUserEntity() {
+    }
+
+    public MyUserEntity(String uname, Long i) {
+        username = uname;
+        id = i;
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public List<AddressEntity> getAddresses() {
+        return addresses;
+    }
+
+    public void setAddresses(List<AddressEntity> addresses) {
+        this.addresses = addresses;
+    }
+
+    public String getUsername() {
+        return username;
+    }
+
+    public void setUsername(String username) {
+        this.username = username;
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/MyUserEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/TestCriteriaInitialization.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/TestCriteriaInitialization.java?rev=1626287&view=auto
==============================================================================
--- openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/TestCriteriaInitialization.java (added)
+++ openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/TestCriteriaInitialization.java Fri Sep 19 17:59:00 2014
@@ -0,0 +1,76 @@
+/*
+ * 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.criteria.init;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.TypedQuery;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Root;
+
+import org.apache.openjpa.persistence.test.SingleEMFTestCase;
+
+public class TestCriteriaInitialization extends SingleEMFTestCase {
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp(CLEAR_TABLES, AddressEntity.class, AddressPk.class, MyUserEntity.class);
+        EntityManager em = emf.createEntityManager();
+        try {
+            em.getTransaction().begin();
+
+            em.persist(new MyUserEntity("wayne", Long.valueOf(1)));
+            em.persist(new MyUserEntity("garth", Long.valueOf(2)));
+
+            em.persist(new AddressEntity(new AddressPk("street_1", Long.valueOf(1))));
+            em.persist(new AddressEntity(new AddressPk("street_2", Long.valueOf(2))));
+
+            em.getTransaction().commit();
+        } finally {
+            if (em.getTransaction().isActive()) {
+                em.getTransaction().rollback();
+            }
+            em.close();
+        }
+    }
+
+    public void test() {
+        emf.close();
+
+        EntityManagerFactory oldEmf = emf;
+        emf = createEMF(AddressEntity.class, AddressPk.class, MyUserEntity.class);
+        // ensure that we get a fresh emf
+        assertNotEquals(oldEmf, emf);
+        emf.getCriteriaBuilder();
+        EntityManager em = emf.createEntityManager();
+        try {
+            CriteriaQuery<MyUserEntity> cq = em.getCriteriaBuilder().createQuery(MyUserEntity.class);
+            Root<MyUserEntity> from = cq.from(MyUserEntity.class);
+            CriteriaQuery<MyUserEntity> selectAll = cq.select(from);
+            TypedQuery<MyUserEntity> query = em.createQuery(selectAll);
+            List<MyUserEntity> res = query.getResultList();
+            // Make sure we get two results
+            assertEquals(2, res.size());
+        } finally {
+            em.close();
+        }
+    }
+}

Propchange: openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/TestCriteriaInitialization.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java?rev=1626287&r1=1626286&r2=1626287&view=diff
==============================================================================
--- openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java (original)
+++ openjpa/trunk/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java Fri Sep 19 17:59:00 2014
@@ -43,6 +43,7 @@ import org.apache.openjpa.lib.conf.Value
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.lib.util.Closeable;
 import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.meta.MetaDataRepository;
 import org.apache.openjpa.persistence.criteria.CriteriaBuilderImpl;
 import org.apache.openjpa.persistence.criteria.OpenJPACriteriaBuilder;
 import org.apache.openjpa.persistence.meta.MetamodelImpl;
@@ -342,8 +343,10 @@ public class EntityManagerFactoryImpl
 
     public MetamodelImpl getMetamodel() {
         if (_metaModel == null) {
-            _metaModel = new MetamodelImpl(getConfiguration()
-                .getMetaDataRepositoryInstance());
+            MetaDataRepository mdr = getConfiguration().getMetaDataRepositoryInstance();
+            mdr.setValidate(MetaDataRepository.VALIDATE_RUNTIME, true);
+            mdr.setResolve(MetaDataRepository.MODE_MAPPING_INIT, true);
+            _metaModel = new MetamodelImpl(mdr);
         }
         return _metaModel;
     }