mirror of https://github.com/apache/openjpa.git
OPENJPA-2505 : Properly init MetaDataRepository when obtaining a reference in EntityManagerFactory.getMetaModel.
git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1626287 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3eaaa0c4b2
commit
86c478ac07
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -43,6 +43,7 @@ import org.apache.openjpa.lib.conf.Value;
|
||||||
import org.apache.openjpa.lib.log.Log;
|
import org.apache.openjpa.lib.log.Log;
|
||||||
import org.apache.openjpa.lib.util.Closeable;
|
import org.apache.openjpa.lib.util.Closeable;
|
||||||
import org.apache.openjpa.lib.util.Localizer;
|
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.CriteriaBuilderImpl;
|
||||||
import org.apache.openjpa.persistence.criteria.OpenJPACriteriaBuilder;
|
import org.apache.openjpa.persistence.criteria.OpenJPACriteriaBuilder;
|
||||||
import org.apache.openjpa.persistence.meta.MetamodelImpl;
|
import org.apache.openjpa.persistence.meta.MetamodelImpl;
|
||||||
|
@ -342,8 +343,10 @@ public class EntityManagerFactoryImpl
|
||||||
|
|
||||||
public MetamodelImpl getMetamodel() {
|
public MetamodelImpl getMetamodel() {
|
||||||
if (_metaModel == null) {
|
if (_metaModel == null) {
|
||||||
_metaModel = new MetamodelImpl(getConfiguration()
|
MetaDataRepository mdr = getConfiguration().getMetaDataRepositoryInstance();
|
||||||
.getMetaDataRepositoryInstance());
|
mdr.setValidate(MetaDataRepository.VALIDATE_RUNTIME, true);
|
||||||
|
mdr.setResolve(MetaDataRepository.MODE_MAPPING_INIT, true);
|
||||||
|
_metaModel = new MetamodelImpl(mdr);
|
||||||
}
|
}
|
||||||
return _metaModel;
|
return _metaModel;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue