diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressEntity.java new file mode 100644 index 000000000..6383d0f5f --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressEntity.java @@ -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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressPk.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressPk.java new file mode 100644 index 000000000..a0eadf588 --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/AddressPk.java @@ -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; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/MyUserEntity.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/MyUserEntity.java new file mode 100644 index 000000000..0edb49f7b --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/MyUserEntity.java @@ -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 addresses = new ArrayList(); + + 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 getAddresses() { + return addresses; + } + + public void setAddresses(List addresses) { + this.addresses = addresses; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } +} diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/TestCriteriaInitialization.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/TestCriteriaInitialization.java new file mode 100644 index 000000000..bcce0c79e --- /dev/null +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/criteria/init/TestCriteriaInitialization.java @@ -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 cq = em.getCriteriaBuilder().createQuery(MyUserEntity.class); + Root from = cq.from(MyUserEntity.class); + CriteriaQuery selectAll = cq.select(from); + TypedQuery query = em.createQuery(selectAll); + List res = query.getResultList(); + // Make sure we get two results + assertEquals(2, res.size()); + } finally { + em.close(); + } + } +} diff --git a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java index d145353ca..43ceb4ead 100644 --- a/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java +++ b/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/EntityManagerFactoryImpl.java @@ -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; }