Question

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 ?

Was it helpful?

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top