2016-04-29 145 views
0

我有这个类Karaf 4.0.5休眠错误

import org.hibernate.Session; 

import javax.persistence.EntityManager; 
import javax.persistence.TypedQuery; 
import javax.persistence.criteria.CriteriaBuilder; 
import javax.persistence.criteria.CriteriaQuery; 
import javax.persistence.criteria.Root; 
import java.util.List; 

public class BaseJpaDao<E> implements BaseDao<E>{ 
    protected Class<?> entityClass; 

    private EntityManager entityManager; 

    public BaseJpaDao(Class<?> entityClass, EntityManager entityManager) { 
     this.entityClass = entityClass; 
     this.entityManager = entityManager; 
    } 

    @Override 
    public E persist(E e) { 
     entityManager.persist(e); 
     return e; 
    } 

    @Override 
    public E get(Long id) { 
     return (E) entityManager.find(entityClass, id); 
    } 

    @Override 
    public E get(Integer id) { 
     return (E) entityManager.find(entityClass, id); 
    } 

    @Override 
    public List<E> getAll() { 
     CriteriaBuilder cb = entityManager.getCriteriaBuilder(); 
     CriteriaQuery<E> cq = cb.createQuery((Class<E>) entityClass); 
     Root<E> rootEntry = cq.from((Class<E>) entityClass); 
     CriteriaQuery<E> all = cq.select(rootEntry); 
     TypedQuery<E> allQuery = entityManager.createQuery(all); 
     return allQuery.getResultList(); 
//  return entityManager.createCriteria(entityClass).list(); 
//  return new ArrayList<>(); 
    } 

    @Override 
    public E merge(E e) { 
     return entityManager.merge(e); 
    } 

    @Override 
    public void remove(E e) { 
     entityManager.remove(e); 
    } 

    @Override 
    public void remove(Long id) { 
     E e = get(id); 
     entityManager.remove(e); 
    } 

    @Override 
    public void saveAll(List<E> entityList) { 
     for (E entity : entityList) { 
      entityManager.merge(entity); 
     } 

     entityManager.flush(); 
    } 

    @Override 
    public EntityManager getEntityManager() { 
     return entityManager; 
    } 

    @Override 
    public Session getSession() { 
     Session unwrap = null; 
     unwrap = entityManager.unwrap(Session.class); 
     return unwrap; 
    } 

} 

当我开始我的karaf它开始不错。但重新启动后,我有错误

[email protected]>ERROR: Bundle domain.payment.module.impl [254] Error starting mvn:domain/payment-module-impl/1.0.0 (org.osgi.framework.BundleException: Uses constraint violation. Unable to re 
solve resource domain.payment.module.impl [domain.payment.module.impl [254](R 254.0)] because it is exposed to package 'javax.persistence' from resources org.apache.geronimo.specs.geroni 
mo-jpa_2.0_spec [org.apache.geronimo.specs.geronimo-jpa_2.0_spec [106](R 106.0)] and javax.persistence [javax.persistence [31](R 31.0)] via two dependency chains. 

Chain 1: 
    domain.payment.module.impl [domain.payment.module.impl [254](R 254.0)] 
    import: (&(osgi.wiring.package=javax.persistence)(version>=1.1.0)(!(version>=2.0.0))) 
    | 
    export: osgi.wiring.package: javax.persistence 
    org.apache.geronimo.specs.geronimo-jpa_2.0_spec [org.apache.geronimo.specs.geronimo-jpa_2.0_spec [106](R 106.0)] 

Chain 2: 
    domain.payment.module.impl [domain.payment.module.impl [254](R 254.0)] 
    import: (&(osgi.wiring.package=org.hibernate)(version>=4.2.0)(!(version>=5.0.0))) 
    | 
    export: osgi.wiring.package=org.hibernate; uses:=javax.persistence 
    org.hibernate.core [org.hibernate.core [204](R 204.0)] 
    import: (osgi.wiring.package=javax.persistence) 
    | 
    export: osgi.wiring.package: javax.persistence 
    javax.persistence [javax.persistence [31](R 31.0)] Unresolved requirements: [[domain.payment.module.impl [254](R 254.0)] osgi.wiring.package; (&(osgi.wiring.package=org.hibernate)(version>=4. 
2.0)(!(version>=5.0.0)))]) 
org.osgi.framework.BundleException: Uses constraint violation. Unable to resolve resource domain.payment.module.impl [domain.payment.module.impl [254](R 254.0)] because it is exposed to 
package 'javax.persistence' from resources org.apache.geronimo.specs.geronimo-jpa_2.0_spec [org.apache.geronimo.specs.geronimo-jpa_2.0_spec [106](R 106.0)] and javax.persistence [javax.persistence [31 
](R 31.0)] via two dependency chains. 

Chain 1: 
    domain.payment.module.impl [domain.payment.module.impl [254](R 254.0)] 
    import: (&(osgi.wiring.package=javax.persistence)(version>=1.1.0)(!(version>=2.0.0))) 
    | 
    export: osgi.wiring.package: javax.persistence 
    org.apache.geronimo.specs.geronimo-jpa_2.0_spec [org.apache.geronimo.specs.geronimo-jpa_2.0_spec [106](R 106.0)] 

Chain 2: 
    domain.payment.module.impl [domain.payment.module.impl [254](R 254.0)] 
    import: (&(osgi.wiring.package=org.hibernate)(version>=4.2.0)(!(version>=5.0.0))) 
    | 
    export: osgi.wiring.package=org.hibernate; uses:=javax.persistence 
    org.hibernate.core [org.hibernate.core [204](R 204.0)] 
    import: (osgi.wiring.package=javax.persistence) 
    | 
    export: osgi.wiring.package: javax.persistence 
    javax.persistence [javax.persistence [31](R 31.0)] Unresolved requirements: [[domain.payment.module.impl [254](R 254.0)] osgi.wiring.package; (&(osgi.wiring.package=org.hibernate)(version>=4. 
2.0)(!(version>=5.0.0)))] 
     at org.apache.felix.framework.Felix.resolveBundleRevision(Felix.java:4111) 
     at org.apache.felix.framework.Felix.startBundle(Felix.java:2117) 
     at org.apache.felix.framework.Felix.setActiveStartLevel(Felix.java:1371) 
     at org.apache.felix.framework.FrameworkStartLevelImpl.run(FrameworkStartLevelImpl.java:308) 
     at java.lang.Thread.run(Thread.java:745) 

但如果我删除类这个方法,所有进口的地方使用

@Override 
     public Session getSession() { 
      Session unwrap = null; 
      unwrap = entityManager.unwrap(Session.class); 
      return unwrap; 
     } 

休眠进口一切工作的罚款。 karaf 4.0.5 在karaf 3.0.6中一切正常。

回答

0

使用约束的问题在于您安装了两个不同的jpa规范捆绑包。

我想一个是来自你自己的功能,一个来自karaf功能。你应该或者引用与karaf相同的bundle,或者最好将bundle标记为dependency =“true”。这允许卡拉夫决定是否需要它。