Search code examples
hibernatehbm2ddl

hbm2ddl ignores @Column annotation?


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 ?


Solution

  • 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.