冬眠&春天,标识符无效

时间:2017-08-18 10:43:51

标签: spring hibernate

我在spring mvc环境中使用hibernate orm来坚持处理数据库。

我有一些桌子;但我不会告诉你我的桌子(如果你想,我会编辑这篇文章)

问题是,当hibernate运行时,它会生成sql - 我可以通过配置“hbm2_ddl auto”来查看sql - 但是sql具有无效的标识符。

select newsreplie0_.news_article# as news6_3_4_, newsreplie0_.reply# as reply1_4_, 
newsreplie0_.reply# as reply1_4_3_, newsreplie0_.account_account# as account5_4_3_, 
newsreplie0_.content as content4_3_, newsreplie0_.dt as dt4_3_, 
newsreplie0_.news_article# as news6_4_3_, newsreplie0_.reply_at as reply4_4_3_, 
account1_.account# as account1_0_0_, account1_.email as email0_0_, 
account1_.passwd as passwd0_0_, accountpro2_.account# as account1_1_1_, 
accountpro2_.nickname as nickname1_1_, accountsec3_.account# as account1_2_2_, 
accountsec3_.activate_key as activate2_2_2_, accountsec3_.activated as activated2_2_, 
accountsec3_.enabled as enabled2_2_, accountsec3_.login_failed as login5_2_2_ 
from news_reply newsreplie0_ 
left outer join 
cookingstep.account account1_ on newsreplie0_.account_account#=account1_.account# 
left outer join 
cookingstep.account_profile accountpro2_ on account1_.account#=accountpro2_.account# 
left outer join 
cookingstep.account_security accountsec3_ on account1_.account#=accountsec3_.account# 
where newsreplie0_.news_article#=9 
{FAILED after 4 msec}

上面的语句是hibernate生成的一个sql。错误是:

java.sql.SQLSyntaxErrorException: 
ORA-00904: "NEWSREPLIE0_"."ACCOUNT_ACCOUNT#": Invalid Identifier

在该异常消息中,有一个名为“ACCOUNT_ACCOUNT#”的列。 它应该只是“帐户#”,而不是“ACCOUNT _”。

那么,如何删除这个词?

修改

谢谢大家的回复。我以前曾问过类似的问题。 我检查了那篇文章,似乎问题是@JoinColumn注释丢失了。现在它已经解决了。

这是我的实体。

Account.java用于用户信息

package com.musicovery12.cookingstep.persistence.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;


@Entity
@Table(name="account", catalog="cookingstep", uniqueConstraints= {
        @UniqueConstraint(columnNames="email")
})
public class Account implements Serializable{

    private static final long serialVersionUID = 1L;

    private int accountId;
    private String email;
    private String password;
    private Set<UserRole> userRoles = new HashSet<UserRole>(0);
    private AccountProfile profile;
    private AccountSecurity security;
    private Set<News> newsList;
    private Set<NewsReply> newsReplyList;

    public Account() {}

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_account")
    @SequenceGenerator(name="seq_account", sequenceName="seq_account", allocationSize=1)
    @Column(name="account#", unique=true, nullable=false)
    public int getAccountId() {
        return accountId;
    }

    public void setAccountId(int accountId) {
        this.accountId = accountId;
    }

    @Column(name="email", unique=true, nullable=false)
    public String getEmail() {
        return email;
    }

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

    @Column(name="passwd", nullable=false)
    public String getPassword() {
        return password;
    }

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

    @OneToMany(mappedBy="pk.account", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    public Set<UserRole> getUserRoles() {
        return userRoles;
    }

    public void setUserRoles(Set<UserRole> userRoles) {
        this.userRoles = userRoles;
    }

    @OneToOne(mappedBy="account", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    public AccountProfile getProfile() {
        return profile;
    }

    public void setProfile(AccountProfile profile) {
        this.profile = profile;
    }

    @OneToOne(mappedBy="account", fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    public AccountSecurity getSecurity() {
        return security;
    }

    public void setSecurity(AccountSecurity security) {
        this.security = security;
    }

    @OneToMany(mappedBy="account", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    public Set<News> getNewsList() {
        return newsList;
    }

    public void setNewsList(Set<News> newsList) {
        this.newsList = newsList;
    }

    @OneToMany(mappedBy="account", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    public Set<NewsReply> getNewsReplyList() {
        return newsReplyList;
    }

    public void setNewsReplyList(Set<NewsReply> newsReplyList) {
        this.newsReplyList = newsReplyList;
    }




}

和NewsReply.java用于新闻社区文章的回复列表。

package com.musicovery12.cookingstep.persistence.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name="news_reply")
public class NewsReply {

    private int replyId;
    private News news;
    private Date date;
    private String content;
    private Account account;
    private int replyAt;


    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="gen_seq")
    @SequenceGenerator(name="gen_seq", sequenceName="gen_seq", allocationSize=1)
    @Column(name="reply#", unique=true, nullable=false)
    public int getReplyId() {
        return replyId;
    }
    public void setReplyId(int replyId) {
        this.replyId = replyId;
    }

    @Temporal(TemporalType.DATE)
    @Column(name="dt")
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }

    @Column(name="content", nullable=false)
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }

    @Column(name="reply_at")
    public int getReplyAt() {
        return replyAt;
    }
    public void setReplyAt(int replyAt) {
        this.replyAt = replyAt;
    }

    @ManyToOne
    public News getNews() {
        return news;
    }
    public void setNews(News news) {
        this.news = news;
    }

    @ManyToOne
    @JoinColumn(name="account#", referencedColumnName="account#")
    public Account getAccount() {
        return account;
    }
    public void setAccount(Account account) {
        this.account = account;
    }
}
在NewsReply.java中,没有JoinColumn注释来指向前导键列名。

谢谢。

1 个答案:

答案 0 :(得分:0)

@ManyToOne
@JoinColumn(name="account#", referencedColumnName="account#")
public Account getAccount() {
    return account;
}

这就是问题,你告诉hibernate这个表的技术名称为account#,这是不允许的。

你可以做的是强制hibernate使用#通过定义

@ManyToOne
@JoinColumn(name="`account#`", referencedColumnName="`account#`")
public Account getAccount() {
    return account;
}

但这是糟糕的风格,你也必须在拥有方面做到这一点。

为什么不让hibernate为你创建实体?他更精确!