代码之家  ›  专栏  ›  技术社区  ›  Seric

更改Naturald参数名称后出错

  •  1
  • Seric  · 技术社区  · 6 年前

    我使用liquibase更改了自然主义列名(从“name”改为“query\u name”):

    <changeSet id="rename_name_to_query_name">
        <preConditions onFail="CONTINUE">
            <columnExists tableName="pm_dbqueries" columnName="name" />
            <not>
                <columnExists tableName="pm_dbqueries" columnName="query_name" />
            </not>
        </preConditions>
        <renameColumn
            columnDataType="varchar(40)"
            newColumnName="query_name"
            oldColumnName="name"
            tableName="pm_dbqueries"/>
    </changeSet>
    

    它工作正常,名称已更改。。。我还更新了java中的实体类:

    @Entity
    @Table( name = "pm_dbqueries", uniqueConstraints = { @UniqueConstraint(columnNames = "id"),
                                                          @UniqueConstraint(columnNames = "query_name") } )
    @SequenceGenerator( name = "pm_dbqueries_seq", sequenceName = "pm_dbqueries_seq" )
    public class DBQuery
    {   
        @Id
        @GeneratedValue( strategy = GenerationType.AUTO, generator = "pm_dbqueries_seq" )
        @Column(name = "id", unique = true, nullable = false)
        private Long id;
    
        @NaturalId
        @Column(name = "query_name", unique = true, nullable = false, length = 40)
        private String queryName;
    
        @Column(name = "query", unique = false, nullable = false, length = 4000)
        private String query;
    
        public Long getId()
        {
            return id;
        }
    
        public void setId( Long id )
        {
            this.id = id;
        }
    
        public String getQueryName()
        {
            return queryName;
        }
    
        public void setQueryName( String queryName )
        {
            this.queryName = queryName;
        }
    
        public String getQuery()
        {
            return query;
        }
    
        public void setQuery( String query )
        {
            this.query = query;
        }
    }
    

    但问题是。。。当我尝试执行此操作时:

    return (DBQuery) sessionFactory.getCurrentSession().byNaturalId(DBQuery.class).using( "query_name", name ).load();
    

    显示错误:

    org.hibernate.HibernateException: No value specified for natural-id property XXX.DBQuery#queryName
        at org.hibernate.event.spi.ResolveNaturalIdEvent.<init>(ResolveNaturalIdEvent.java:102)
        at org.hibernate.event.spi.ResolveNaturalIdEvent.<init>(ResolveNaturalIdEvent.java:52)
        at org.hibernate.internal.SessionImpl$BaseNaturalIdLoadAccessImpl.resolveNaturalId(SessionImpl.java:2466)
        at org.hibernate.internal.SessionImpl$NaturalIdLoadAccessImpl.load(SessionImpl.java:2581)
    

    在这些变化之前,它工作得很好。有什么建议吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   blkpingu aaquib    6 年前

    尝试此操作,因为您需要在此处使用属性名称而不是列名。它工作得更早,因为两者之间没有区别(name==name)。

    return (DBQuery) sessionFactory.getCurrentSession().byNaturalId(DBQuery.class).using( "queryName", name ).load();