简体   繁体   中英

JPA can't find table

I'm using JPA 2.0, Eclipselink 2.0, maven and weblogic 10.3.6. Here's my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="certifications" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>jdbc/com/ni/ds_edata_soa_nontx</jta-data-source>
        <class>com.ni.apps.engineering.certification.entities.NicdsCliCertificationStg</class>
        <class>com.ni.apps.engineering.certification.entities.NicdsCliCertificationStgPK</class>
        <class>com.ni.apps.engineering.certification.entities.NicdsCliUpMapping</class>   
        <properties>                        
            <property name="javax.persistence.jdbc.password" value="ni"/>
            <property name="javax.persistence.jdbc.user" value="NI"/>
            <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
            <property name="eclipselink.logging.level.sql" value="FINE"/>       
        </properties>       
    </persistence-unit>
</persistence>

This is my facade

    package com.ni.apps.engineering.certification.controller;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;
import javax.persistence.TypedQuery;
import com.ni.apps.engineering.certification.entities.NicdsCliCertificationStg;
import com.ni.apps.engineering.certification.exception.CertificationException;
import com.ni.apps.engineering.certification.utils.CertificationConstants;

public class CertificationFacade {  

    @PersistenceUnit(unitName = "certifications")
    private EntityManagerFactory emf;

    public List<NicdsCliCertificationStg> getCertificationsByUpId(String upId)
            throws CertificationException {
        String stringQuery = new StringBuilder(
                "select c from NicdsCliCertificationStg c where c.id.contactsId = 1234").toString();
                //.append(upId).toString();
        try {
            emf = Persistence.createEntityManagerFactory("certifications");     
            EntityManager em = emf.createEntityManager();
            TypedQuery<NicdsCliCertificationStg> query = em
                    .createQuery(stringQuery, NicdsCliCertificationStg.class);
            return query.getResultList();
        } catch (Exception e) {
            throw new CertificationException(
                    CertificationConstants.INTERNAL_ERROR_MESSAGE, e);
        }
    }
}

When I try to run the app, this is the error I'm getting

Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.1.v20111018-r10243): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist

Error Code: 942
Call: SELECT CONTACTS_ID, PROFILE_ID FROM NI.NICDS_CLI_UP_MAPPING WHERE (PROFILE_ID = ?)
    bind => [1 parameter bound]
Query: ReadAllQuery(referenceClass=NicdsCliUpMapping sql="SELECT CONTACTS_ID, PROFILE_ID FROM NI.NICDS_CLI_UP_MAPPING WHERE (PROFILE_ID = ?)")

Any ideas why it can't find the table?

-EDIT- Adding the entity

package com.ni.apps.engineering.certification.entities;

import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;


/**
 * The persistent class for the NICDS_CLI_CERTIFICATION_STG database table.
 * 
 */
@Entity
@Table(name="NICDS_CLI_CERTIFICATION_STG", schema="NI")
public class NicdsCliCertificationStg implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private NicdsCliCertificationStgPK id;

    @Column(name="ALTERNATE_EMAIL")
    private String alternateEmail;

    @Temporal(TemporalType.DATE)
    @Column(name="CERT_EXPIRATION_DATE")
    private Date certExpirationDate;

    @Temporal(TemporalType.DATE)
    @Column(name="CERT_ISSUE_DATE")
    private Date certIssueDate;

    @Column(name="CERT_STATUS")
    private String certStatus;

    @Column(name="FIRST_NAME")
    private String firstName;

    @Column(name="LAST_NAME")
    private String lastName;

    @Column(name="PRIMARY_EMAIL")
    private String primaryEmail;

    //bi-directional one-to-one association to NicdsCliUpMapping
    @OneToOne
    @JoinColumn(name="CONTACTS_ID")
    private NicdsCliUpMapping nicdsCliUpMapping;

    public NicdsCliCertificationStg() {
    }

    public NicdsCliCertificationStgPK getId() {
        return this.id;
    }

    public void setId(NicdsCliCertificationStgPK id) {
        this.id = id;
    }

    public String getAlternateEmail() {
        return this.alternateEmail;
    }

    public void setAlternateEmail(String alternateEmail) {
        this.alternateEmail = alternateEmail;
    }

    public Date getCertExpirationDate() {
        return this.certExpirationDate;
    }

    public void setCertExpirationDate(Date certExpirationDate) {
        this.certExpirationDate = certExpirationDate;
    }

    public Date getCertIssueDate() {
        return this.certIssueDate;
    }

    public void setCertIssueDate(Date certIssueDate) {
        this.certIssueDate = certIssueDate;
    }

    public String getCertStatus() {
        return this.certStatus;
    }

    public void setCertStatus(String certStatus) {
        this.certStatus = certStatus;
    }

    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPrimaryEmail() {
        return this.primaryEmail;
    }

    public void setPrimaryEmail(String primaryEmail) {
        this.primaryEmail = primaryEmail;
    }

    public NicdsCliUpMapping getNicdsCliUpMapping() {
        return this.nicdsCliUpMapping;
    }

    public void setNicdsCliUpMapping(NicdsCliUpMapping nicdsCliUpMapping) {
        this.nicdsCliUpMapping = nicdsCliUpMapping;
    }


}

package com.ni.apps.engineering.certification.entities;

import java.io.Serializable;
import javax.persistence.*;

/**
 * The primary key class for the NICDS_CLI_CERTIFICATION_STG database table.
 * 
 */
@Embeddable
public class NicdsCliCertificationStgPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="CONTACTS_ID")
    private long contactsId;

    @Column(name="CERT_NAME")
    private String certName;

    public NicdsCliCertificationStgPK() {
    }
    public long getContactsId() {
        return this.contactsId;
    }
    public void setContactsId(long contactsId) {
        this.contactsId = contactsId;
    }
    public String getCertName() {
        return this.certName;
    }
    public void setCertName(String certName) {
        this.certName = certName;
    }

    public boolean equals(Object other) {
        if (this == other) {
            return true;
        }
        if (!(other instanceof NicdsCliCertificationStgPK)) {
            return false;
        }
        NicdsCliCertificationStgPK castOther = (NicdsCliCertificationStgPK)other;
        return 
            (this.contactsId == castOther.contactsId)
            && this.certName.equals(castOther.certName);
    }

    public int hashCode() {
        final int prime = 31;
        int hash = 17;
        hash = hash * prime + ((int) (this.contactsId ^ (this.contactsId >>> 32)));
        hash = hash * prime + this.certName.hashCode();

        return hash;
    }
}

package com.ni.apps.engineering.certification.entities;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;


/**
 * The persistent class for the NICDS_CLI_UP_MAPPING database table.
 * 
 */
@Entity
@Table(name="NICDS_CLI_UP_MAPPING", schema="NI")
public class NicdsCliUpMapping implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="NICDS_CLI_UP_MAPPING_CONTACTSID_GENERATOR" )
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="NICDS_CLI_UP_MAPPING_CONTACTSID_GENERATOR")
    @Column(name="CONTACTS_ID")
    private long contactsId;

    @Column(name="PROFILE_ID")
    private BigDecimal profileId;

    //bi-directional one-to-one association to NicdsCliCertificationStg
    @OneToOne(mappedBy="nicdsCliUpMapping")
    private NicdsCliCertificationStg nicdsCliCertificationStg;

    public NicdsCliUpMapping() {
    }

    public long getContactsId() {
        return this.contactsId;
    }

    public void setContactsId(long contactsId) {
        this.contactsId = contactsId;
    }

    public BigDecimal getProfileId() {
        return this.profileId;
    }

    public void setProfileId(BigDecimal profileId) {
        this.profileId = profileId;
    }

    public NicdsCliCertificationStg getNicdsCliCertificationStg() {
        return this.nicdsCliCertificationStg;
    }

    public void setNicdsCliCertificationStg(NicdsCliCertificationStg nicdsCliCertificationStg) {
        this.nicdsCliCertificationStg = nicdsCliCertificationStg;
    }


}

The error was that the weblogic's datasource had a different combination of user and password than the one I need for the application's schema. If I find a way to force JPA to use a specific schema/user/password combination without changing the user in the datasource, I'll update this answer.

If you are using different user to test, then the user that is configured in the datasource

In the datasource Connection Pool config, go to advanced section

Type this in the "Init SQL" section (see the SQL word before the instruction, its needed)

SQL ALTER SESSION SET CURRENT_SCHEMA=DEVLPMT

See http://docs.oracle.com/middleware/1212/wls/JDBCA/jdbc_datasources.htm#CJAFCBBD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM