简体   繁体   中英

JPA entity class and SQL query to retrieve Data from MySQL

I am developing java desktop application using JPA to connect to database. I having problem to retrieve entities to list from on of my Tables in Mysql, I have done it successfully with other Tables. But this one got foreign key and i get errors.

entity class:

package databaseController;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;


    @Entity
    public class Specific {

        private String SPECIFIC_NAME;
        private int SAUCE_ID;
        private String SPECIFIC_TOPPINGS;
        private int SPECIFIC_P_PRICE;

        private int SPECIFIC_S_PRICE;
        private int SPECIFIC_M_PRICE;
        private int SPECIFIC_L_PRICE;
        private int SPECIFIC_XL_PRICE;


        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private int SPECIFIC_ID;

        @ManyToOne
        @JoinColumn(name = "SAUCE_ID")
        private Sauce souce;

        public String getSPECIFIC_NAME() {
            return SPECIFIC_NAME;
        }

        public void setSPECIFIC_NAME(String sPECIFIC_NAME) {
            SPECIFIC_NAME = sPECIFIC_NAME;
        }

        public int getSAUCE_ID() {
            return SAUCE_ID;
        }

        public void setSAUCE_ID(int sAUCE_ID) {
            SAUCE_ID = sAUCE_ID;
        }

        public String getSPECIFIC_TOPPINGS() {
            return SPECIFIC_TOPPINGS;
        }

        public void setSPECIFIC_TOPPINGS(String sPECIFIC_TOPPINGS) {
            SPECIFIC_TOPPINGS = sPECIFIC_TOPPINGS;
        }

        public int getSPECIFIC_P_PRICE() {
            return SPECIFIC_P_PRICE;
        }

        public void setSPECIFIC_P_PRICE(int sPECIFIC_P_PRICE) {
            SPECIFIC_P_PRICE = sPECIFIC_P_PRICE;
        }

        public int getSPECIFIC_S_PRICE() {
            return SPECIFIC_S_PRICE;
        }

        public void setSPECIFIC_S_PRICE(int sPECIFIC_S_PRICE) {
            SPECIFIC_S_PRICE = sPECIFIC_S_PRICE;
        }

        public int getSPECIFIC_M_PRICE() {
            return SPECIFIC_M_PRICE;
        }

        public void setSPECIFIC_M_PRICE(int sPECIFIC_M_PRICE) {
            SPECIFIC_M_PRICE = sPECIFIC_M_PRICE;
        }

        public int getSPECIFIC_L_PRICE() {
            return SPECIFIC_L_PRICE;
        }

        public void setSPECIFIC_L_PRICE(int sPECIFIC_L_PRICE) {
            SPECIFIC_L_PRICE = sPECIFIC_L_PRICE;
        }

        public int getSPECIFIC_XL_PRICE() {
            return SPECIFIC_XL_PRICE;
        }

        public void setSPECIFIC_XL_PRICE(int sPECIFIC_XL_PRICE) {
            SPECIFIC_XL_PRICE = sPECIFIC_XL_PRICE;
        }

        public int getSPECIFIC_ID() {
            return SPECIFIC_ID;
        }

        public void setSPECIFIC_ID(int sPECIFIC_ID) {
            SPECIFIC_ID = sPECIFIC_ID;
        }

        public Sauce getSouce() {
            return souce;
        }

        public void setSouce(Sauce souce) {
            this.souce = souce;
        }


    }

Foreign Key entity (other table entity):

package databaseController;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Sauce {
    private String SAUCE_NAME;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int SAUCE_ID;

    public String getSAUCE_NAME() {
        return SAUCE_NAME;
    }
    public void setSAUCE_NAME(String sAUCE_NAME) {
        SAUCE_NAME = sAUCE_NAME;
    }
    public int getSAUCE_ID() {
        return SAUCE_ID;
    }
    public void setSAUCE_ID(int sAUCE_ID) {
        SAUCE_ID = sAUCE_ID;
    }
}

and in my controller I am trying to retrieve data from Specific Table using:

private List<Specific> special = em.createQuery("SELECT e FROM Specific e").getResultList();

Please have a look, I am sure thats something to do with that foreign key connection...

error I am getting:

Exception in thread "AWT-EventQueue-0" Local Exception Stack: 
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@71bd8993
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [as] failed.
Internal Exception: Exception [EclipseLink-7250] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: [class databaseController.Specific] uses a non-entity [class databaseController.Sauce] as target entity in the relationship attribute [field souce].
    at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(PersistenceProvider.java:107)
    at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:177)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
    at databaseController.DatabaseController.<init>(DatabaseController.java:21)
    .....

I guess the problem is related to SAUCE_ID field which is declared twice in the entity:

@Entity
public class Specific {
    private int SAUCE_ID;
    ...
    @JoinColumn(name = "SAUCE_ID")
    @ManyToOne
    private Sauce souce;
}

Try to rename the foreign key, ie

@Entity
public class Specific {
    ...
    @JoinColumn(name = "SAUCE_FK")
    @ManyToOne
    private Sauce souce;
}

EDIT : also make sure Sauce entity is included in persistence.xml

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