4

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 ?

Chris Milburn
  • 862
  • 1
  • 12
  • 20
  • What table structure do you expect to be generated? I see 3 `@Column` annotations (with names) and 3 DB table columns with the same names. It looks like it is working to me. – Jesse Webb Jul 17 '12 at 14:40
  • I expect the case to match (Im porting to a case sensitive database) however even if the column names are totally different this dosnt work. – Chris Milburn Jul 18 '12 at 07:54
  • Sorry I didn't even notice the case difference, oops my bad. – Jesse Webb Jul 18 '12 at 13:48

2 Answers2

5

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.

axtavt
  • 239,438
  • 41
  • 511
  • 482
0

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.

Chris Milburn
  • 862
  • 1
  • 12
  • 20