@Column注解不起作用 [英] @Column annotation does not work

查看:3474
本文介绍了@Column注解不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我刚开始Hibernate。我有点困惑为什么 @Column 注释在getter上不起作用。据我所知,它适用于领域和getter,但不适用于setter。

  @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;


userName 数据库中的列更改,from name getter附加到它的值中。



主类:

  public static void main(String [] args){
// TODO自动生成的方法存根

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();


解决方案

由同一实体中的混合访问模式引起。如果您想混合使用访问模式,您必须执行以下步骤:


  1. 定义默认访问模式,如下所示: / b>

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


使用此设置,默认访问模式设置为字段访问。如果您不这样做,并且使用映射注释(例如@Column)注释字段以及属性,则不会定义行为。


  1. 现在注明属性访问的getter方法(即告诉持久性提供者,我已将默认值定义为字段访问,但在这种情况下,我想使用属性访问):

      @Access(AccessType.PROPERTY)
    @Column(name =USER_NAME)
    public String getUserName(){
    返回用户名+from name getter;
    }


  2. 最后,使用@Transient注解将相应的字段标记为瞬态以便提供者不会尝试使用该字段以及该属性来坚持该状态:

      @Transient private String userName ; 


注意:如果没有正当理由使用混合模式时,应坚持现场访问模式或属性访问模式;不要在实体中使用这两种模式,因为您想编写简洁易懂的代码! (我的个人意见)



最后在你的代码中对以下行进行评论:

  @Entity(name =USER_DETAILS)

如果您使用 name 属性故意为实体命名,它可能不是问题;但如果您试图将实体映射到名为 USER_DETAILS 的表,这不是正确的方法;请使用以下内容:

  @Table(name =USER_DETAILS)


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();
}

解决方案

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")

这篇关于@Column注解不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆