hbm2ddl ignores @Column annotation?

江枫思渺然 提交于 2020-01-14 13:10:54

问题


Why would hbm2ddl ignore the @Column annotation ?

This is my class :-

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "BASETEMPLATE")
public class BaseTemplate implements IBaseTemplate
{
    private Integer id;

    @Column(name="TEMPLATENAME")    
    private String templateName;

    @Column(name="BASETEMPLATEID")  
    private Integer baseTemplateId;

    @Id 
    @GeneratedValue 
    @Column(name = "TEMPLATEID")
    @Override
    /** {@inheritDoc} */
    public Integer getId() { return id; }       
...
}

and hbm2dll generates this (sqlserver) table

dbo.BASETEMPLATE 
(
TEMPLATEID      int
templateName    varchar(255)
baseTemplateId  int
)

dialect is org.hibernate.dialect.SQLServerDialect Strangely the primary key is always created correctly ?


回答1:


When you place annotations on getters, Hibernate uses property access strategy, when you place them on fields, Hibernate uses field access strategy. However, you should not mix these strategies in the same entity (or, more precisely, in the same inheritance hierarchy), unless you use @Access for fine-grained control over access strategy.

By default Hibernate expects annotations to be placed in the same way as @Id, therefore in your case it expects annotations on getters.




回答2:


I dont know why the @Column on a field is being ignored by hbm2ddl but Ive found if you annotate the getter instead it correctly sets the column name in the table.



来源:https://stackoverflow.com/questions/11524276/hbm2ddl-ignores-column-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!