在spring-data-jpa模式创建中忽略@Column或@Basic JPA批注 [英] @Column or @Basic JPA annotations ignored in spring-data-jpa schema creation

查看:251
本文介绍了在spring-data-jpa模式创建中忽略@Column或@Basic JPA批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对我期望开箱即用的东西感到完全困惑.因此,要么我做错了什么,要么只是误解.

I am utterly confused by something I expected to work just out of the box. So either I am doing something totally wrong or this is just a misunderstanding.

我正在尝试在JPA实体类中使用getter/setter批注.我坚持在JPA Wiki上找到的示例(s. http://en.wikibooks .org/wiki/Java_Persistence/Basic_Attributes#Conversion ).该示例如下所示:

I am trying to have a getter/setter annotation in a JPA Entity class. I sticked to an example I found on the JPA wiki (s. http://en.wikibooks.org/wiki/Java_Persistence/Basic_Attributes#Conversion). The example looks as follows:

    @Entity
    public class Employee {
        ...
        private boolean isActive;
        ...
        @Transient
        public boolean getIsActive() {
            return isActive;
        }
        public void setIsActive(boolean isActive) {
            this.isActive = isActive;
        }
        @Basic
        private String getIsActiveValue() {
            if (isActive) {
                return "T";
            } else {
                return "F";
            }
        }
        private void setIsActiveValue(String isActive) {
            this.isActive = "T".equals(isActive);
        }
    }

我以最清楚,最干净的spring-data-jpa示例为例: http ://spring.io/guides/gs/accessing-data-jpa/.

I took the clearest and cleanest spring-data-jpa example I could find: http://spring.io/guides/gs/accessing-data-jpa/.

我从git中签出并更改了示例实体类(例如

I checked it out from git and changed their example entity class (s. https://github.com/spring-guides/gs-accessing-data-jpa/blob/master/complete/src/main/java/hello/Customer.java) to look as follows:

@Entity
public class Customer {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;
    private String firstName;
    private String lastName;

    protected Customer() {}

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }

    @Transient
    private boolean isActive;

    @Transient
    public boolean getIsActive() {
        return isActive;
    }
    public void setIsActive(boolean isActive) {
        this.isActive = isActive;
    }

    @Column
    private String getIsActiveValue() {
        if (isActive) {
            return "T";
        } else {
            return "F";
        }
    }
    private void setIsActiveValue(String isActive) {
        this.isActive = "T".equals(isActive);
    }

}

现在什么都没有改变.不会创建相应的字符串字段.创建数据库表的日志中的行仍然如下所示:

Now nothing changes. The respective String-Field does not get created. The line from the log creating the database table still looks as follows:

17:11:10.540 [main] DEBUG o.h.tool.hbm2ddl.SchemaUpdate - create table Customer (id bigint generated by default as identity, firstName varchar(255), lastName varchar(255), primary key (id))

我完全不知道这可能是什么原因.我找不到找不到有关spring-data-jpa不允许在getter上进行注释的文档.

I have absolutely no idea on what could be the reason for this. I could find not documentation that spring-data-jpa would not allow for annotations on getters.

任何帮助将不胜感激!

推荐答案

如果您希望它与'@Transient'注释一起使用,则应按照Andrei的建议进行操作,可以为isActiveValue添加一个额外的字段,但这是最重要的进行连续注释,否则您将获得无法预测的行为.

If you want it to work with the '@Transient' annotation you should do as Andrei suggests, you could add an extra field for isActiveValue but it is most important to annotate consistently otherwise you will get unpredictable behavior.

在注释字段与属性(getter和setter)时,将有所作为.

When annotating fields versus properties (getters and setters) it will make a difference.

在您的情况下,您似乎想要在getter中执行一些逻辑,因此注释字段可能不会获得所需的结果.我并不特别喜欢逻辑,但了解需要对吸气剂进行注释.

In your case it looks like you want to do some logic in the getter hence annotating a field will likely not have the desired result. I don't particularly like the logic but understand that there is a need to annotate a getter.

考虑上面代码中的逻辑,我将简单地完全消除该字段上的瞬变,并将带有批注的逻辑放入getter和setter中.

Considering the logic in your code above I would simply eliminate the transient on the field altogether and put the logic with the annotations in the getters and setters.

@Entity
@Table(name = "Customer")
public class Customer {

    private static final String IS_ACTIVE = "T";

    private long id;
    private String firstName;
    private String lastName;
    private String isActive = "";

    protected Customer() {}

    public Customer(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Column(unique = true, nullable = false)
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(unique = true, nullable = false)
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column(unique = true, nullable = false)
    public String getIsActive() {
        return isActive;
    }

    public void setIsActive(String isActive) {
        this.isActive = isActive;
    }

    @Transient
    public boolean isActive() {
        return isActive.equals(IS_ACTIVE);
    }

    @Override
    public String toString() {
        return String.format(
                "Customer[id=%d, firstName='%s', lastName='%s']",
                id, firstName, lastName);
    }
}

这篇关于在spring-data-jpa模式创建中忽略@Column或@Basic JPA批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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