Java Hibernate:在我的类中添加一个未映射到 db-table 的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4008418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:17:00  来源:igfitidea点击:

Hibernate: Add a property in my class that is not mapped to a db-table

javahibernateannotationshibernate-annotations

提问by Adnan

I have a table tbl_skythat has 2 properties nameand modeland I would use Hibernate annotation like;

我有一个表tbl_sky有2个属性namemodel我会使用Hibernate注释等;

@Entity
@Table(name="tbl_sky")
public class Sky implements Serializable {
    private String name;
    private String model;
    private String status;

    @Id
    public String getName() {
        return name;
    }
.
.
.

But I need to add one more property statusthat does not exist in the table but is needed in the class. How could I declare that property so that I have it in my class but not in my db-table?

但是我需要再添加一个status在表中不存在但在类中需要的属性。我如何声明该属性,以便我在班级中拥有它,但在我的数据库表中却没有?

All help is appreciated.

感谢所有帮助。

采纳答案by Kel

Use @Transientannotation for field you are not going to store in DB:

@Transient对不打算存储在数据库中的字段使用注释:

@Transient
public String getStatus() {
    return status;
}

or:

或者:

@Transient
private String status;

回答by jjungnickel

If you annotate a field with @Transientit will not be persisted.

如果你用@Transient它注释一个字段,它不会被持久化。

回答by Sean Patrick Floyd

Mark it as @Transient, and it won't be part of the DB schema.

将其标记为@Transient,它不会成为数据库架构的一部分。