使用Hibernate 5.1.2作为JPA供应商

时间:2016-11-08 12:16:33

标签: hibernate jpa weblogic12c java-6

我试图从使用JPA 1.0到Hibernate 5.1.2的OpenJPA实现1.0.1升级我的应用程序

我试图在Entity类上调用geResultList时遇到nullpointer异常。

我的示例类如下:(我已经更改了类名,因为我无法共享原始源代码。但映射是相同的。)

1)类充电器(代表一种具有2针的移动充电器)

@Entity
@Table(name = "Charger")
class ChargerEntityExt5 extends ChargerEntity1Abstract implements Serializable {

@Transient
private static final long serialVersionUID = -1L;

@OneToMany(mappedBy="chargerEntityExt5",targetEntity=PortEntityExt5.class,
        fetch=FetchType.EAGER)
@Fetch(value = FetchMode.SUBSELECT)
public List<PinEntityExt5> pinEntityExtList;

public List<PinEntityExt5> getPinEntityExtList() {
    return pinEntityExtList;
}

public void setPinEntityExtList(List<PinEntityExt5> pinEntityExtList) {
    this.pinEntityExtList = pinEntityExtList;
}

 }

2)

@MappedSuperclass
class ChargerEntity1Abstract implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name="ID")
public Long Id;

@Column(name="CHARGER_ID")
public String chargerId;


// *** Constructor ****
public ChargerEntity1Abstract() {
}

@Override
public String toString() {
    return "ChargerEntity [cpeId=" + Id!=null? Id.toString():""  + 
            ", chargerId=" + chargerId!=null? chargerId.toString():""  + "]";
}

public Long getId() {
    return Id;
}

public void setId(Long id) {
    Id = id;
}

public String getChargerId() {
    return chargerId;
}

public void setChargerId(String chargerId) {
    this.chargerId = chargerId;
}

 }

3)每个充电器都有2个引脚,因此我宣布充电器和引脚之间的双向一对多映射。

@Entity
@Table(name = "PIN")
class PinEntityExt5 extends PinEntity1Abstract implements Serializable{

@Transient
private static final long serialVersionUID = -1L;

@ManyToOne(optional=false)
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name="CHARGER_ID", referencedColumnName="CHARGER_ID",  insertable= false, updatable = false)
private ChargerEntityExt5 chargerEntityExt5;

public ChargerEntityExt5 getChargerEntityExt5() {
    return chargerEntityExt5;
}

public void setChargerEntityExt5(ChargerEntityExt5 chargerEntityExt5) {
    this.chargerEntityExt5 = chargerEntityExt5;
}

}

4)PIN实体有2个属性。 CHARGER_ID和PIN_NUMBER。这2字段唯一地定义了PIN表中的一条记录。所以我已经声明了一个IDclass(ChargerPin)来表示复合键

@MappedSuperclass
@IdClass(ChargerPin.class)
class PinEntity1Abstract implements Serializable{

@Transient
private static final long serialVersionUID = 1L;

@Id
@Column(name="CHARGER_ID", nullable = true)
public String chargerId;

@Id
@Column(name="PIN_NUMBER", nullable = true)
public Long pin;

public PinEntity1Abstract() {
}


@Override
public String toString() {
    return "PinEntity [chargerId=" + chargerId + 
            ", port=" + pin!=null? pin.toString():""  + "]";
}


public String getChargerId() {
    return chargerId;
}


public void setChargerId(String chargerId) {
    this.chargerId = chargerId;
}


public Long getPin() {
    return pin;
}


public void setPin(Long pin) {
    this.pin = pin;
}

}



class ChargerPin implements java.io.Serializable{

@Transient
private static final long serialVersionUID = 1L;

private String chargerId;

private Long pin;

// **** Constructor *****
public ChargerPin() {
}

public ChargerPin(String kapId, Long port) {
    this.chargerId = kapId;
    this.pin = port;
}



@Override
public String toString() {
    return "kapId [chargerId=" + chargerId!=null? chargerId.toString():"" +
            ", port=" + pin!=null? pin.toString():""  + "]";
}

@Override
public int hashCode() {

    final int prime = 31;
    int result = 1;
    int kap = chargerId != null ?(Integer.parseInt(chargerId)):0;
    int kap_port = pin!=null?(pin).intValue():0;
    result = prime * result + kap;
    result = prime * result + kap_port;
    return result;

}

@Override
public boolean equals(Object obj) {
    //return ((o instanceof CpePort) && (kapId.equalsIgnoreCase(((CpePort) o).getKapId())) && (port == (((CpePort) o).getPort())));
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (!(obj instanceof CpePort1))
        return false;
    ChargerPin other = (ChargerPin) obj;
    if (chargerId == null) {
        if (other.chargerId != null)
            return false;
    } else if ((other.chargerId != null) && chargerId.equals(other.chargerId)) {
        return false;
    }

    if (pin == null) {
        if (other.pin != null)
            return false;
    } else if ((other.pin != null) && (pin != other.pin))
        return false;

    return true;
}

public String getChargerId() {
    return chargerId;
}

public void setChargerId(String chargerId) {
    this.chargerId = chargerId;
}

public Long getPin() {
    return pin;
}

public void setPin(Long pin) {
    this.pin = pin;
}

}

5)现在,当我调用下面的查询时,我得到了一个带有空的satck跟踪的nullPointer异常。

public class HibernateTestClass {

/**
 * @param args
 */

public static EntityManagerFactory entityManagerFactory;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    EntityManager em = null;

    List<ChargerEntityExt5> ChargerList = null;

    Query query = null;
    String queryString = "SELECT ce FROM ChargerEntityExt5 ce WHERE ce.chargerId=?1";
    try {

        try {
            em = createEntityManager();
        } catch (Exception e) {
            System.out.println(e);
        }

        query = em.createQuery(queryString);
        query.setParameter(1, "110284026");
        ChargerList = query.getResultList();
        System.out.println(ChargerList.size());

    } catch (IllegalStateException e) {
        System.out.println(e);
    } catch (IllegalArgumentException e) {
        System.out.println(e);
    } catch (Exception e) {
        System.out.println(e); 
    } finally{
        em.close();
    }

    }

public static EntityManager createEntityManager() {

    entityManagerFactory =     Persistence.createEntityManagerFactory("polcaincapu");
    return entityManagerFactory.createEntityManager();
}
}

你能不能看看这个bug。 早些时候我正在尝试使用Hibernate 5.1.1。版本然后代码抛出错误为&#34;错误访问字段通过反射持久属性&#34;。 然后我更改了代码库以使用Hibernate版本5.1.2,然后在不更改源代码的情况下将异常更改为Nullpointer异常。

向下投票

这是异常的完整堆栈跟踪。你知道为什么会这样吗:

显示java.lang.NullPointerException在ChargerEntity1Abstract.toString(EmbeddedIDTester.java:462)在org.hibernate.pretty.MessageHelper.addIdToCollectionInfoString(MessageHelper.java:362)在org.hibernate.pretty.MessageHelper.collectionInfoString(MessageHelper.java :333)在org.hibernate.loader.plan.exec.process.internal.CollectionReferenceInitializerImpl.finishUpRow(CollectionReferenceInitializerImpl.java:62)在org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java :121)在org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:122)在org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122 )在org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)在org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollec tionInitializer.java:88)在org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688)在org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75)在org.hibernate.internal .SessionImpl.initializeCollection(SessionImpl.java:2004)在org.hibernate.collection.internal.AbstractPersistentCollection $ 4.doWork(AbstractPersistentCollection.java:567)在org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:249) org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:563)org.hibernate.engine.Stateful.StatefulPersistenceContext.initializeNonLazyCollections中的org.hibernate.collection.internal.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:731) (StatefulPersistenceContext.java:918)org.hibernate。 loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:149)位于org.hibernate.loader.entity的org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86)。 plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167)org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4002)atg.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508)位于org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad的org.hibernate.event.infaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478 (DefaultLoadEventListener.java:278)org.hibernate.event.internal.DefaultLoadEven上的org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121) tlistener.onLoad(DefaultLoadEventListener.java:89)位于org.hibernate的org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1142)org.hibernate.internal.SessionImpl.access $ 2600(SessionImpl.java:167)。 internal.SessionImpl $ IdentifierLoadAccessImpl.doLoad(SessionImpl.java:2762)在org.hibernate.internal.SessionImpl $ IdentifierLoadAccessImpl.load(SessionImpl.java:2741)在org.hibernate.internal.SessionImpl.get(SessionImpl.java:978)在org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1075)在org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1033)在dk.tdc.visitation.polcalink.resource.dbResource .polcadb.inca.servingfp.EmbeddedIDTester.main(EmbeddedIDTester.java:47)

0 个答案:

没有答案