Hibernate @Column 注释不起作用

Posted

技术标签:

【中文标题】Hibernate @Column 注释不起作用【英文标题】:Hibernate @Column annotation not work 【发布时间】:2017-06-03 15:12:39 【问题描述】:

我有 SqlServer 2008 数据库服务器,我使用 spring boot + jpa(hibernate) 访问数据库。

我已经在实体属性访问方法中添加了@Column注解,如:

@Basic
@Column(name = "AdminPickDate", nullable = true)
public Timestamp getAdminPickDate() 
    return adminPickDate;

并且hibernate sql输出显示列名没有使用注解@Colume中的名称。

Sql 输出:

Hibernate: select ddforumart0_.articleid as articlei1_95_0_, ddforumart0_.admin_pick_date as admin_pi2_95_0_, ddforumart0_.article_title as article_3_95_0_, ddforumart0_.article_type as article_4_95_0_, ddforumart0_.at_who as at_who5_95_0_, ddforumart0_.brief as brief6_95_0_, ddforumart0_.classifyid as classify7_95_0_, ddforumart0_.classify_title as classify8_95_0_, ddforumart0_.come_from as come_fro9_95_0_, ddforumart0_.comment_count as comment10_95_0_, ddforumart0_.comment_date as comment11_95_0_, ddforumart0_.comment_enable as comment12_95_0_, ddforumart0_.config as config13_95_0_, ddforumart0_.content as content14_95_0_, ddforumart0_.create_by as create_15_95_0_, ddforumart0_.create_date as create_16_95_0_, ddforumart0_.del_flag as del_fla17_95_0_, ddforumart0_.img_url as img_url18_95_0_, ddforumart0_.is_anonymous as is_anon19_95_0_, ddforumart0_.is_del_allow as is_del_20_95_0_, ddforumart0_.is_hot as is_hot21_95_0_, ddforumart0_.isqa as isqa22_95_0_, ddforumart0_.is_top as is_top23_95_0_, ddforumart0_.like_count as like_co24_95_0_, ddforumart0_.modify_by as modify_25_95_0_, ddforumart0_.modify_date as modify_26_95_0_, ddforumart0_.pick_date as pick_da27_95_0_, ddforumart0_.plateid as plateid28_95_0_, ddforumart0_.plate_title as plate_t29_95_0_, ddforumart0_.qatype as qatype30_95_0_, ddforumart0_.tags as tags31_95_0_, ddforumart0_.user_code as user_co32_95_0_, ddforumart0_.user_infoid as user_in33_95_0_, ddforumart0_.user_name as user_na34_95_0_, ddforumart0_.view_count as view_co35_95_0_ from dd_forum_article ddforumart0_ where ddforumart0_.articleid=?

我的代码中是否有一些错误的用法?

我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.didi.home.dao</groupId>
    <artifactId>didi-home-dao</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>didi-home-dao</name>
    <description>didi home data producer</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.didi.home.model</groupId>
            <artifactId>didi-home-model</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application.yml

spring:
  datasource:
    url: jdbc:sqlserver://1.2.3.4:1433;databaseName=DB_DiDiWeb
    username: u
    password: mypwd
  jpa:
    show-sql: true
    hibernate:
      naming:
#        strategy: org.hibernate.cfg.EJB3NamingStrategy
#        strategy: org.hibernate.cfg.DefaultComponentSafeNamingStrategy
#        strategy: org.hibernate.cfg.DefaultNamingStrategy
#        strategy: org.hibernate.cfg.ImprovedNamingStrategy
#        strategy: org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy

这里是一个演示库:DdForumArticleRepository

package com.my.home.dao;

import com.didi.home.model.DdForumArticle;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;

/**
 * Created by jacks808@163.com on 2017/1/13.
 */
@Component
public interface DdForumArticleRepository extends JpaRepository<DdForumArticle, Integer> 

实体代码:

package com.didi.home.model;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.sql.Timestamp;

/**
 * Created by jacks808@163.com on 2017/1/13.
 */
// , schema = "dbo", catalog = "DB_DiDiWeb"
@Entity
@Table(name = "DD_Forum_Article")
public class DdForumArticle 
    private int articleId;
    private Integer articleType;
    private String articleTitle;
    private String content;
    private String brief;
    private String atWho;
    private String imgUrl;
    private String tags;
    private String config;
    private Integer viewCount;
    private Integer likeCount;
    private Integer commentCount;
    private Integer commentEnable;
    private Timestamp commentDate;
    private Integer isHot;
    private Integer isTop;
    private Integer isQa;
    private String qaType;
    private Timestamp pickDate;
    private Timestamp adminPickDate;
    private Integer plateId;
    private String plateTitle;
    private Integer classifyId;
    private String classifyTitle;
    private Integer userInfoId;
    private String userName;
    private String userCode;
    private Integer delFlag;
    private String createBy;
    private Timestamp createDate;
    private String modifyBy;
    private Timestamp modifyDate;
    private Integer isAnonymous;
    private Integer isDelAllow;
    private String comeFrom;

    @Id
    @Column(name = "ArticleID", nullable = false)
    public int getArticleId() 
        return articleId;
    

    public void setArticleId(int articleId) 
        this.articleId = articleId;
    

    @Basic
    @Column(name = "ArticleType", nullable = true)
    public Integer getArticleType() 
        return articleType;
    

    public void setArticleType(Integer articleType) 
        this.articleType = articleType;
    

    @Basic
    @Column(name = "ArticleTitle", nullable = true, length = 100)
    public String getArticleTitle() 
        return articleTitle;
    

    public void setArticleTitle(String articleTitle) 
        this.articleTitle = articleTitle;
    

    @Basic
    @Column(name = "Content", nullable = true, length = 2147483647)
    public String getContent() 
        return content;
    

    public void setContent(String content) 
        this.content = content;
    

    @Basic
    @Column(name = "Brief", nullable = true, length = 255)
    public String getBrief() 
        return brief;
    

    public void setBrief(String brief) 
        this.brief = brief;
    

    @Basic
    @Column(name = "AtWho", nullable = true, length = 2147483647)
    public String getAtWho() 
        return atWho;
    

    public void setAtWho(String atWho) 
        this.atWho = atWho;
    

    @Basic
    @Column(name = "ImgUrl", nullable = true, length = 2147483647)
    public String getImgUrl() 
        return imgUrl;
    

    public void setImgUrl(String imgUrl) 
        this.imgUrl = imgUrl;
    

    @Basic
    @Column(name = "Tags", nullable = true, length = 255)
    public String getTags() 
        return tags;
    

    public void setTags(String tags) 
        this.tags = tags;
    

    @Basic
    @Column(name = "Config", nullable = true, length = 2147483647)
    public String getConfig() 
        return config;
    

    public void setConfig(String config) 
        this.config = config;
    

    @Basic
    @Column(name = "ViewCount", nullable = true)
    public Integer getViewCount() 
        return viewCount;
    

    public void setViewCount(Integer viewCount) 
        this.viewCount = viewCount;
    

    @Basic
    @Column(name = "LikeCount", nullable = true)
    public Integer getLikeCount() 
        return likeCount;
    

    public void setLikeCount(Integer likeCount) 
        this.likeCount = likeCount;
    

    @Basic
    @Column(name = "CommentCount", nullable = true)
    public Integer getCommentCount() 
        return commentCount;
    

    public void setCommentCount(Integer commentCount) 
        this.commentCount = commentCount;
    

    @Basic
    @Column(name = "CommentEnable", nullable = true)
    public Integer getCommentEnable() 
        return commentEnable;
    

    public void setCommentEnable(Integer commentEnable) 
        this.commentEnable = commentEnable;
    

    @Basic
    @Column(name = "CommentDate", nullable = true)
    public Timestamp getCommentDate() 
        return commentDate;
    

    public void setCommentDate(Timestamp commentDate) 
        this.commentDate = commentDate;
    

    @Basic
    @Column(name = "IsHot", nullable = true)
    public Integer getIsHot() 
        return isHot;
    

    public void setIsHot(Integer isHot) 
        this.isHot = isHot;
    

    @Basic
    @Column(name = "IsTop", nullable = true)
    public Integer getIsTop() 
        return isTop;
    

    public void setIsTop(Integer isTop) 
        this.isTop = isTop;
    

    @Basic
    @Column(name = "IsQA", nullable = true)
    public Integer getIsQa() 
        return isQa;
    

    public void setIsQa(Integer isQa) 
        this.isQa = isQa;
    

    @Basic
    @Column(name = "QAType", nullable = true, length = 10)
    public String getQaType() 
        return qaType;
    

    public void setQaType(String qaType) 
        this.qaType = qaType;
    

    @Basic
    @Column(name = "PickDate", nullable = true)
    public Timestamp getPickDate() 
        return pickDate;
    

    public void setPickDate(Timestamp pickDate) 
        this.pickDate = pickDate;
    

    @Basic
    @Column(name = "AdminPickDate", nullable = true)
    public Timestamp getAdminPickDate() 
        return adminPickDate;
    

    public void setAdminPickDate(Timestamp adminPickDate) 
        this.adminPickDate = adminPickDate;
    

    @Basic
    @Column(name = "PlateID", nullable = true)
    public Integer getPlateId() 
        return plateId;
    

    public void setPlateId(Integer plateId) 
        this.plateId = plateId;
    

    @Basic
    @Column(name = "PlateTitle", nullable = true, length = 32)
    public String getPlateTitle() 
        return plateTitle;
    

    public void setPlateTitle(String plateTitle) 
        this.plateTitle = plateTitle;
    

    @Basic
    @Column(name = "ClassifyID", nullable = true)
    public Integer getClassifyId() 
        return classifyId;
    

    public void setClassifyId(Integer classifyId) 
        this.classifyId = classifyId;
    

    @Basic
    @Column(name = "ClassifyTitle", nullable = true, length = 32)
    public String getClassifyTitle() 
        return classifyTitle;
    

    public void setClassifyTitle(String classifyTitle) 
        this.classifyTitle = classifyTitle;
    

    @Basic
    @Column(name = "UserInfoID", nullable = true)
    public Integer getUserInfoId() 
        return userInfoId;
    

    public void setUserInfoId(Integer userInfoId) 
        this.userInfoId = userInfoId;
    

    @Basic
    @Column(name = "UserName", nullable = true, length = 32)
    public String getUserName() 
        return userName;
    

    public void setUserName(String userName) 
        this.userName = userName;
    

    @Basic
    @Column(name = "UserCode", nullable = true, length = 32)
    public String getUserCode() 
        return userCode;
    

    public void setUserCode(String userCode) 
        this.userCode = userCode;
    

    @Basic
    @Column(name = "DelFlag", nullable = true)
    public Integer getDelFlag() 
        return delFlag;
    

    public void setDelFlag(Integer delFlag) 
        this.delFlag = delFlag;
    

    @Basic
    @Column(name = "CreateBy", nullable = true, length = 32)
    public String getCreateBy() 
        return createBy;
    

    public void setCreateBy(String createBy) 
        this.createBy = createBy;
    

    @Basic
    @Column(name = "CreateDate", nullable = true)
    public Timestamp getCreateDate() 
        return createDate;
    

    public void setCreateDate(Timestamp createDate) 
        this.createDate = createDate;
    

    @Basic
    @Column(name = "ModifyBy", nullable = true, length = 32)
    public String getModifyBy() 
        return modifyBy;
    

    public void setModifyBy(String modifyBy) 
        this.modifyBy = modifyBy;
    

    @Basic
    @Column(name = "ModifyDate", nullable = true)
    public Timestamp getModifyDate() 
        return modifyDate;
    

    public void setModifyDate(Timestamp modifyDate) 
        this.modifyDate = modifyDate;
    

    @Basic
    @Column(name = "IsAnonymous", nullable = true)
    public Integer getIsAnonymous() 
        return isAnonymous;
    

    public void setIsAnonymous(Integer isAnonymous) 
        this.isAnonymous = isAnonymous;
    

    @Basic
    @Column(name = "IsDelAllow", nullable = true)
    public Integer getIsDelAllow() 
        return isDelAllow;
    

    public void setIsDelAllow(Integer isDelAllow) 
        this.isDelAllow = isDelAllow;
    

    @Basic
    @Column(name = "ComeFrom", nullable = true, length = 32)
    public String getComeFrom() 
        return comeFrom;
    

    public void setComeFrom(String comeFrom) 
        this.comeFrom = comeFrom;
    

    @Override
    public boolean equals(Object o) 
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        DdForumArticle that = (DdForumArticle) o;

        if (articleId != that.articleId) return false;
        if (articleType != null ? !articleType.equals(that.articleType) : that.articleType != null) return false;
        if (articleTitle != null ? !articleTitle.equals(that.articleTitle) : that.articleTitle != null) return false;
        if (content != null ? !content.equals(that.content) : that.content != null) return false;
        if (brief != null ? !brief.equals(that.brief) : that.brief != null) return false;
        if (atWho != null ? !atWho.equals(that.atWho) : that.atWho != null) return false;
        if (imgUrl != null ? !imgUrl.equals(that.imgUrl) : that.imgUrl != null) return false;
        if (tags != null ? !tags.equals(that.tags) : that.tags != null) return false;
        if (config != null ? !config.equals(that.config) : that.config != null) return false;
        if (viewCount != null ? !viewCount.equals(that.viewCount) : that.viewCount != null) return false;
        if (likeCount != null ? !likeCount.equals(that.likeCount) : that.likeCount != null) return false;
        if (commentCount != null ? !commentCount.equals(that.commentCount) : that.commentCount != null) return false;
        if (commentEnable != null ? !commentEnable.equals(that.commentEnable) : that.commentEnable != null)
            return false;
        if (commentDate != null ? !commentDate.equals(that.commentDate) : that.commentDate != null) return false;
        if (isHot != null ? !isHot.equals(that.isHot) : that.isHot != null) return false;
        if (isTop != null ? !isTop.equals(that.isTop) : that.isTop != null) return false;
        if (isQa != null ? !isQa.equals(that.isQa) : that.isQa != null) return false;
        if (qaType != null ? !qaType.equals(that.qaType) : that.qaType != null) return false;
        if (pickDate != null ? !pickDate.equals(that.pickDate) : that.pickDate != null) return false;
        if (adminPickDate != null ? !adminPickDate.equals(that.adminPickDate) : that.adminPickDate != null)
            return false;
        if (plateId != null ? !plateId.equals(that.plateId) : that.plateId != null) return false;
        if (plateTitle != null ? !plateTitle.equals(that.plateTitle) : that.plateTitle != null) return false;
        if (classifyId != null ? !classifyId.equals(that.classifyId) : that.classifyId != null) return false;
        if (classifyTitle != null ? !classifyTitle.equals(that.classifyTitle) : that.classifyTitle != null)
            return false;
        if (userInfoId != null ? !userInfoId.equals(that.userInfoId) : that.userInfoId != null) return false;
        if (userName != null ? !userName.equals(that.userName) : that.userName != null) return false;
        if (userCode != null ? !userCode.equals(that.userCode) : that.userCode != null) return false;
        if (delFlag != null ? !delFlag.equals(that.delFlag) : that.delFlag != null) return false;
        if (createBy != null ? !createBy.equals(that.createBy) : that.createBy != null) return false;
        if (createDate != null ? !createDate.equals(that.createDate) : that.createDate != null) return false;
        if (modifyBy != null ? !modifyBy.equals(that.modifyBy) : that.modifyBy != null) return false;
        if (modifyDate != null ? !modifyDate.equals(that.modifyDate) : that.modifyDate != null) return false;
        if (isAnonymous != null ? !isAnonymous.equals(that.isAnonymous) : that.isAnonymous != null) return false;
        if (isDelAllow != null ? !isDelAllow.equals(that.isDelAllow) : that.isDelAllow != null) return false;
        if (comeFrom != null ? !comeFrom.equals(that.comeFrom) : that.comeFrom != null) return false;

        return true;
    

    @Override
    public int hashCode() 
        int result = articleId;
        result = 31 * result + (articleType != null ? articleType.hashCode() : 0);
        result = 31 * result + (articleTitle != null ? articleTitle.hashCode() : 0);
        result = 31 * result + (content != null ? content.hashCode() : 0);
        result = 31 * result + (brief != null ? brief.hashCode() : 0);
        result = 31 * result + (atWho != null ? atWho.hashCode() : 0);
        result = 31 * result + (imgUrl != null ? imgUrl.hashCode() : 0);
        result = 31 * result + (tags != null ? tags.hashCode() : 0);
        result = 31 * result + (config != null ? config.hashCode() : 0);
        result = 31 * result + (viewCount != null ? viewCount.hashCode() : 0);
        result = 31 * result + (likeCount != null ? likeCount.hashCode() : 0);
        result = 31 * result + (commentCount != null ? commentCount.hashCode() : 0);
        result = 31 * result + (commentEnable != null ? commentEnable.hashCode() : 0);
        result = 31 * result + (commentDate != null ? commentDate.hashCode() : 0);
        result = 31 * result + (isHot != null ? isHot.hashCode() : 0);
        result = 31 * result + (isTop != null ? isTop.hashCode() : 0);
        result = 31 * result + (isQa != null ? isQa.hashCode() : 0);
        result = 31 * result + (qaType != null ? qaType.hashCode() : 0);
        result = 31 * result + (pickDate != null ? pickDate.hashCode() : 0);
        result = 31 * result + (adminPickDate != null ? adminPickDate.hashCode() : 0);
        result = 31 * result + (plateId != null ? plateId.hashCode() : 0);
        result = 31 * result + (plateTitle != null ? plateTitle.hashCode() : 0);
        result = 31 * result + (classifyId != null ? classifyId.hashCode() : 0);
        result = 31 * result + (classifyTitle != null ? classifyTitle.hashCode() : 0);
        result = 31 * result + (userInfoId != null ? userInfoId.hashCode() : 0);
        result = 31 * result + (userName != null ? userName.hashCode() : 0);
        result = 31 * result + (userCode != null ? userCode.hashCode() : 0);
        result = 31 * result + (delFlag != null ? delFlag.hashCode() : 0);
        result = 31 * result + (createBy != null ? createBy.hashCode() : 0);
        result = 31 * result + (createDate != null ? createDate.hashCode() : 0);
        result = 31 * result + (modifyBy != null ? modifyBy.hashCode() : 0);
        result = 31 * result + (modifyDate != null ? modifyDate.hashCode() : 0);
        result = 31 * result + (isAnonymous != null ? isAnonymous.hashCode() : 0);
        result = 31 * result + (isDelAllow != null ? isDelAllow.hashCode() : 0);
        result = 31 * result + (comeFrom != null ? comeFrom.hashCode() : 0);
        return result;
    

测试代码:

package com.didi.home.dao;

import com.didi.home.model.DdForumArticle;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by jacks808@163.com on 2017/1/13.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class DdForumArticleRepositoryTest 

    @Autowired
    DdForumArticleRepository repository;

    @Test
    public void test() 
        DdForumArticle one = repository.findOne(52);
        System.out.println(one);
    

还有异常堆栈跟踪:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid Column name 'admin_pick_date'
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
    at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
    at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:285)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
    ... 81 more

编辑:添加表架构

CREATE TABLE [dbo].[DD_Forum_Article] (
    [ArticleID] int IDENTITY(1,1) NOT NULL,
    [ArticleType] int NULL,
    [ArticleTitle] nvarchar(100) COLLATE Chinese_PRC_CI_AS NULL,
    [Content] nvarchar(max) COLLATE Chinese_PRC_CI_AS NULL,
    [Brief] nvarchar(255) COLLATE Chinese_PRC_CI_AS NULL,
    [AtWho] nvarchar(max) COLLATE Chinese_PRC_CI_AS NULL,
    [ImgUrl] nvarchar(max) COLLATE Chinese_PRC_CI_AS NULL,
    [Tags] nvarchar(255) COLLATE Chinese_PRC_CI_AS NULL,
    [Config] nvarchar(max) COLLATE Chinese_PRC_CI_AS NULL,
    [ViewCount] int NULL,
    [LikeCount] int NULL,
    [CommentCount] int NULL,
    [CommentEnable] int NULL,
    [CommentDate] datetime NULL,
    [IsHot] int NULL,
    [IsTop] int NULL,
    [IsQA] int NULL,
    [QAType] varchar(10) COLLATE Chinese_PRC_CI_AS NULL,
    [PickDate] datetime NULL,
    [AdminPickDate] datetime NULL,
    [PlateID] int NULL,
    [PlateTitle] nvarchar(32) COLLATE Chinese_PRC_CI_AS NULL,
    [ClassifyID] int NULL,
    [ClassifyTitle] nvarchar(32) COLLATE Chinese_PRC_CI_AS NULL,
    [UserInfoID] int NULL,
    [UserName] nvarchar(32) COLLATE Chinese_PRC_CI_AS NULL,
    [UserCode] nvarchar(32) COLLATE Chinese_PRC_CI_AS NULL,
    [DelFlag] int NULL,
    [CreateBy] nvarchar(32) COLLATE Chinese_PRC_CI_AS NULL,
    [CreateDate] datetime NULL,
    [ModifyBy] nvarchar(32) COLLATE Chinese_PRC_CI_AS NULL,
    [ModifyDate] datetime NULL,
    [IsAnonymous] int NULL DEFAULT ((0)),
    [IsDelAllow] int NULL,
    [ComeFrom] nvarchar(32) COLLATE Chinese_PRC_CI_AS NULL
)
ON [PRIMARY]
TEXTIMAGE_ON [PRIMARY]

【问题讨论】:

这是因为 Spring 没有遵循 JPA 规范。它显式地设置了一些 Hibernate 命名样式,以便它忽略您的 @Column 注释名称。我想知道当像 Spring 这样的软件抛弃可移植性时,为什么 JPA 规范会麻烦地为表/列命名定义标准?该列应为“AdminPickDate”,但在内部使用“Admin_Pick_Date” 谢谢尼尔,有什么办法可以解决这个问题 不使用 Spring?选择与 JPA 规范一致的 Hibernate 命名策略?我使用 DataNucleus,所以获得规范一致的命名,所以无法帮助您了解 Hibernate 细节 我一一尝试了所有这些策略,但没有一个奏效。所以我将它们都添加到这个问题中 【参考方案1】:

通过对应的值检查admin_pick_date字符串字段名称及其数据类型(大写小写)。

祝你好运

【讨论】:

我已经检查了表模式,文件名是正确的。 BTW 表架构已添加到问题正文中。 你的代码的字段名怎么样?错误说你有这样的下划线 admin_pick_date。 您可以在实体代码中看到。我已经将 Column(name='AdminPickDate') 添加到 'getter' 方法中。【参考方案2】:

MSSQL Server 可以根据设置区分大小写

编辑:那是我的错误。你有什么改变吗?

您是否在 application.properties 文件中设置了以下内容?

 spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy

【讨论】:

@Column 注释是由 hibernate-metadata-generator 生成的。并且表模式确实有一个归档的 AdminPickDate(显示在 create table 语句中)。如果您注意到 sql 输出,则 sql 使用 admin_pick_date 来选择数据,而不是在 Column 注释中写入的 'AdminPickDate'。 谢谢你的回答,我已经尝试了所有的命名策略:(org.hibernate.cfg.EJB3NamingStrategy|org.hibernate.cfg.DefaultComponentSafeNamingStrategy|org.hibernate.cfg.DefaultNamingStrategy|org.hibernate. cfg.ImprovedNamingStrategy|org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy) 但是没有人工作 你把那个放在哪里了?你能把那个文件放在这里吗 编辑完成。我一一尝试所有命名策略。还是不行。 这是我项目中的测试代码。 hibernate版本是5.0.11,但是我尝试了EJB3策略,也不行【参考方案3】:

通过将此添加到配置中,我的问题得到了解决:

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

【讨论】:

谢谢你的回答,你能帮我提供描述或链接吗? 【参考方案4】:

是的,接受的答案是正确的。

如果您想进行更多自定义,您可以扩展SpringPhysicalNamingStrategy 并覆盖其中的方法。并且,在配置中选择它作为命名策略的实现。

在代码中:

package app.config;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy;

public class MyNamingStrategy extends SpringPhysicalNamingStrategy 
    @Override
    public boolean isCaseInsensitive(JdbcEnvironment jdbcEnvironment) 
        return false; // MSSQL is case-sensitive
    

    @Override
    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) 
        return name; // we don't need adding underscore to the column name, we just use the name in "@Column(name="xxx")"
    

在 yml 文件中:

spring: 
  jpa:
    properties:
      hibernate.physical_naming_strategy: app.config.MyNamingStrategy

【讨论】:

以上是关于Hibernate @Column 注释不起作用的主要内容,如果未能解决你的问题,请参考以下文章

在 Hibernate 中设置属性的默认值不起作用

Spring @Repository 注释不起作用,但 @Service 注释起作用?

Hibernate 模式参数在 @SequenceGenerator 注释中不起作用

如何告诉 Hibernate 注释 @Column 区分大小写?

事务注释在 Spring Boot 2.1.3 中不起作用

如果在initState()中创建,则Flutter Switch小部件不起作用