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
Hibernate: Add a property in my class that is not mapped to a db-table
提问by Adnan
I have a table tbl_sky
that has 2 properties name
and model
and I would use Hibernate annotation like;
我有一个表tbl_sky
有2个属性name
和model
我会使用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 status
that 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 @Transient
annotation 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 @Transient
it will not be persisted.
如果你用@Transient
它注释一个字段,它不会被持久化。
回答by Sean Patrick Floyd
Mark it as @Transient
, and it won't be part of the DB schema.
将其标记为@Transient
,它不会成为数据库架构的一部分。