2017-08-18 69 views
0

我一直堅持在春天的mvc環境中使用hibernate orm來處理數據庫。hibernate&spring,無效標識符

我有一些表;但我不會告訴你我的表(如果你想,我會編輯這篇文章)

問題是,當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#」,而不是「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註解點foreing鍵列名。

謝謝。

+0

你可以發佈你的ORM映射類嗎? –

+1

什麼是hibernate.hbm2ddl.auto設置爲?驗證,更新,創建,craete-drop?發佈您的實體類的帳戶。駱駝案件可能會欺騙你。 – kkflf

+0

針對ORM映射類的更新文章。和hbm2ddl.auto屬性設置爲驗證 – PLAYMAKER

回答

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爲您創建實體?他更精確!