JPA JPQL查询被转换为大写

时间:2018-07-26 10:54:46

标签: eclipse jpa backend jpql

我目前正在尝试为工作网络应用设置我的JPA后端。但是,当我尝试执行JPQL查询时,它总是将其转换为大写。

例如SELECT u.firstName FROM User将转换为SELECT FIRSTNAME FROM USER。由于表USER不存在,因此将引发异常。

如果我在mySql数据库中创建名为USER的表,则此问题已解决,但我想知道为什么会这样。

用户实体:

@Entity
@NamedQuery(name="User.findAll", query="SELECT u.firstName FROM User u")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int idUser;

    @Lob
    private String educationText;

    private String email;

    private String firstName;

    private int isModerator;

    @Lob
    private String jobExperienceText;

    private String lastName;

    private String password;

    private String phoneNumber;

    private String photoUrl;

    //bi-directional many-to-one association to Advertisment
    @OneToMany(mappedBy="user")
    private List<Advertisment> advertisments;

    //bi-directional many-to-one association to Article
    @OneToMany(mappedBy="user")
    private List<Article> articles;

    //bi-directional many-to-one association to Connection
    @OneToMany(mappedBy="user")
    private List<Connection> connections;

    //bi-directional many-to-one association to ConnectionRequest
    @OneToMany(mappedBy="user")
    private List<ConnectionRequest> connectionRequests;

    //bi-directional many-to-many association to Conversation
    @ManyToMany
    @JoinTable(
        name="ParticipantsOfConversation"
        , joinColumns={
            @JoinColumn(name="User_idUser")
            }
        , inverseJoinColumns={
            @JoinColumn(name="Conversation_idConversation")
            }
        )
    private List<Conversation> conversations;

    //bi-directional one-to-one association to User_has_Skill
    @OneToOne(mappedBy="user")
    private User_has_Skill userHasSkill;

    public User() {
    }

    public int getIdUser() {
        return this.idUser;
    }

    public void setIdUser(int idUser) {
        this.idUser = idUser;
    }

    public String getEducationText() {
        return this.educationText;
    }

    public void setEducationText(String educationText) {
        this.educationText = educationText;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

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

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

    public int getIsModerator() {
        return this.isModerator;
    }

    public void setIsModerator(int isModerator) {
        this.isModerator = isModerator;
    }

    public String getJobExperienceText() {
        return this.jobExperienceText;
    }

    public void setJobExperienceText(String jobExperienceText) {
        this.jobExperienceText = jobExperienceText;
    }

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

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

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhoneNumber() {
        return this.phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getPhotoUrl() {
        return this.photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }

    public List<Advertisment> getAdvertisments() {
        return this.advertisments;
    }

    public void setAdvertisments(List<Advertisment> advertisments) {
        this.advertisments = advertisments;
    }

    public Advertisment addAdvertisment(Advertisment advertisment) {
        getAdvertisments().add(advertisment);
        advertisment.setUser(this);

        return advertisment;
    }

    public Advertisment removeAdvertisment(Advertisment advertisment) {
        getAdvertisments().remove(advertisment);
        advertisment.setUser(null);

        return advertisment;
    }

    public List<Article> getArticles() {
        return this.articles;
    }

    public void setArticles(List<Article> articles) {
        this.articles = articles;
    }

    public Article addArticle(Article article) {
        getArticles().add(article);
        article.setUser(this);

        return article;
    }

    public Article removeArticle(Article article) {
        getArticles().remove(article);
        article.setUser(null);

        return article;
    }

    public List<Connection> getConnections() {
        return this.connections;
    }

    public void setConnections(List<Connection> connections) {
        this.connections = connections;
    }

    public Connection addConnection(Connection connection) {
        getConnections().add(connection);
        connection.setUser(this);

        return connection;
    }

    public Connection removeConnection(Connection connection) {
        getConnections().remove(connection);
        connection.setUser(null);

        return connection;
    }

    public List<ConnectionRequest> getConnectionRequests() {
        return this.connectionRequests;
    }

    public void setConnectionRequests(List<ConnectionRequest> connectionRequests) {
        this.connectionRequests = connectionRequests;
    }

    public ConnectionRequest addConnectionRequest(ConnectionRequest connectionRequest) {
        getConnectionRequests().add(connectionRequest);
        connectionRequest.setUser(this);

        return connectionRequest;
    }

    public ConnectionRequest removeConnectionRequest(ConnectionRequest connectionRequest) {
        getConnectionRequests().remove(connectionRequest);
        connectionRequest.setUser(null);

        return connectionRequest;
    }

    public List<Conversation> getConversations() {
        return this.conversations;
    }

    public void setConversations(List<Conversation> conversations) {
        this.conversations = conversations;
    }

    public User_has_Skill getUserHasSkill() {
        return this.userHasSkill;
    }

    public void setUserHasSkill(User_has_Skill userHasSkill) {
        this.userHasSkill = userHasSkill;
    }

}

Persistnce.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="linkedInProject" transaction-type="RESOURCE_LOCAL">
        <class>model.Advertisment</class>
        <class>model.AdvertismentPK</class>
        <class>model.Advertisment_has_Skill</class>
        <class>model.Advertisment_has_SkillPK</class>
        <class>model.Application</class>
        <class>model.ApplicationPK</class>
        <class>model.Article</class>
        <class>model.ArticlePK</class>
        <class>model.Comment</class>
        <class>model.CommentPK</class>
        <class>model.Connection</class>
        <class>model.ConnectionPK</class>
        <class>model.ConnectionRequest</class>
        <class>model.ConnectionRequestPK</class>
        <class>model.Conversation</class>
        <class>model.Interest</class>
        <class>model.InterestPK</class>
        <class>model.Message</class>
        <class>model.MessagePK</class>
        <class>model.Skill</class>
        <class>model.User</class>
        <class>model.User_has_Skill</class>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/linkedIn"/>
            <property name="javax.persistence.jdbc.user" value="ld"/>
            <property name="javax.persistence.jdbc.password" value="---"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        </properties>
    </persistence-unit>
</persistence>

我收到的例外情况:

Jul 26, 2018 1:01:49 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [servlets.UserServlet] in context with path [/linkedInProject] threw exception
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'linkedIn.USER' doesn't exist
Error Code: 1146
Call: SELECT FIRSTNAME FROM USER
Query: ReportQuery(name="User.findAll" referenceClass=User sql="SELECT FIRSTNAME FROM USER")
    at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:378)
    at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)
    at org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:469)
    at dao.UserDAOImpl.list(UserDAOImpl.java:27)
    at servlets.UserServlet.doGet(UserServlet.java:58)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at filters.EntityManagerInterceptor.doFilter(EntityManagerInterceptor.java:41)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:0)

您可以使用@Table来注释您的实体类,并将表名包装在背景幕中以使其区分大小写:

@Table(name="`User`")
@Entity
@NamedQuery(name="User.findAll", query="SELECT u.firstName FROM User u")