Sumeet Vishwas
Sumeet Vishwas

Reputation: 593

@Column annotation does not work

Currently I have just started Hibernate. I am little confused why @Column annotation do not work on getter. As per i know it works on fields and getter but not on setter. What am i doing wrong?

@Entity (name="USER_DETAILS")
public class UserDetails {

    @Id
    private int userId;

    private String userName;

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    @Column (name="USER_NAME")

    public String getUserName() {
        return userName + " from name getter";
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

Neither userName column change in database nor "from name getter" appends into its value..

Here my main class:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    UserDetails user=new UserDetails();
    user.setUserId(1);
    user.setUserName("First User");
    SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
    Session session=sessionFactory.openSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit();
    session.close();
}

Upvotes: 1

Views: 3864

Answers (3)

Arjun Gautam
Arjun Gautam

Reputation: 329

I was having same problem with @Column(unique=true) I added on application.properties and it worked for me.

resources/application.properties

spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Upvotes: 0

ujulu
ujulu

Reputation: 3309

The problem you are facing is caused by the mixed access modes in the same entity. If you want to mix the access mode you have to do the following steps:

  1. Define the default access mode as follows:

    @Entity
    @Access(AccessType.FIELD)
    public class UserDetails { ...}
    

With this setting the default access mode is set to field access. If you don't do this and annotate fields as well as properties with the mapping annotations (such as @Column) the behaviour is not defined.

  1. Now annotate the the getter method for property access explicitly (that is to tell the persistence provider that I have defined my default to be field access but in this case I want to use property access):

    @Access(AccessType.PROPERTY)
    @Column (name="USER_NAME")
    public String getUserName() {
        return userName+" from name getter";
    }
    
  2. And lastly, mark the corresponding field as transient using @Transient annotation so that the provider will not try to use the field as well as the property to persist the state:

    @Transient private String userName;
    

Note: If there is no valid reason to use mixed mode you should stick with either the field access mode or with the property access mode; don't use both modes in an entity because you want to write clean and understandable code! (my personal opinion)

And one last comment on the following line in your code:

@Entity (name="USER_DETAILS")

In case you're using name attribute deliberately for naming the entity it might not be a problem; but if you are trying to map the entity to a table named USER_DETAILS this is not the right way; instead use the following:

@Table(name = "USER_DETAILS")

Upvotes: 2

Jama A.
Jama A.

Reputation: 16079

As far as I know you need to set hibernate.cfg config to tell hibernate all properties such as datasource and mappings. At the beginning of your main method configure something like this:

   Configuration config = new Configuration().configure("hibernate.cfg.xml"); 
   StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder();
   srb.applySettings(config.getProperties());
   ServiceRegistry registry = srb.build();
   SessionFactory sessionFactory = config.buildSessionFactory(registry);

And your cfg.xml file would be something like:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
        <property name="hibernate.connection.driver_class">YOUR_DRIVER</property>
        <property name="hibernate.connection.username">YOUR_USER</property>
        <property name="hibernate.connection.password">YOUR_PASSWPRD</property>
        <property name="hibernate.connection.url"> YOUR_URL </property>
        <property name="hibernate.dialect">SET_YOUR_DIALECT</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- Map Entity Class -->
    <mapping class="entity.UserDetails"></mapping>

</session-factory>
</hibernate-configuration>

Upvotes: 0

Related Questions