英文:
Using POJO as a base for hibernate entity
问题
我最近开始使用Java进行开发,我的客户也是一名开发人员,自Java发布以来就一直在开发Java应用。
所以当他说“我们有充分的理由不在项目中使用transient字段”的时候,我并没有询问那些理由是什么。但是,回到问题:
我有两个类:
- POJO,仅用于生成JSON:
public class BaseSector implements Serializable {
private String id;
private String name;
private String parentId;
- 实体类:
public class Sector {
@Column(length = 36)
private String id;
@Column(length = 40)
private String name;
@Column(length = 36)
private String parentId;
// ... 其他许多字段
实体类有没有办法扩展这个POJO,并动态添加@Column注解?或者将POJO作为一个接口?或者在POJO的构造函数中使用实体类?
之前我们做过类似这样的事情:
for (Sector sector : sectors) {
BaseSector baseSector = new BaseSector();
baseSector.setId(sector.getId());
baseSector.setName(sector.getName());
baseSector.setParentId(sector.getParentId());
}
但是我通过在HQL构造函数中使用BaseSector来改变了这种做法...
顺便说一下,我们还有SectorInfo和SimpleSectorInfo这两个类也扩展了BaseSector,但那是另外一个话题...
英文:
I started developing in Java quite recently, and my client is also a developer who is developing in Java since it was released.
So when he says "we have a good reason why don't we use transient fields in our project", I didn't ask what those reasons are. But, back to the question:
I have two classes:
- POJO, which is used solely to generate JSON:
public class BaseSector implements Serializable {
private String id;
private String name;
private String parentId;
- Entity:
public class Sector {
@Column(length = 36)
private String id;
@Column(length = 40)
private String name;
@Column(length = 36)
private String parentId;
// ... Bunch of other fields
Is there any way for an Entity class to extend this POJO, and add Column annotations dynamically? Or have POJO as an interface? Or use entity class in POJO constructor?
Earlier we had something like this:
for (Sector sector : sectors) {
BaseSector baseSector = new BaseSector();
baseSector.setId(sector.getId());
baseSector.setName(sector.getName());
baseSector.setParentId(sector.getParentId());
}
But I changed that by using BaseSector in HQL constructor...
Btw, we also have SectorInfo and SimpleSectorInfo which also extend BaseSector, but that's a different subject..
答案1
得分: 0
一个瞬态字段告诉你的实体类,这个特定的字段不应该在数据库中持久化。@Transient
注解用于在 JPA 中忽略不需要持久化到数据库的字段,而 transient
关键字用于在序列化时忽略字段。被 @Transient
注解标记的字段仍然可以被序列化,但是使用 transient
关键字声明的字段既不会被持久化也不会被序列化。
一个普通的 Java 对象(POJO)可以作为实体类的超类,反之亦然。这在 JPA 规范中有说明。你可以在以下链接中找到更多示例:
你可以通过使用注解 @javax.persistence.MappedSuperclass
来实现这一点。
它说明:如果在类级别未使用诸如 @Entity 或 @MappedSuperclass 等与映射相关的注解,则超类将被视为非实体类。
这意味着如果在超类中不使用上述注解,则在这里将把你的超类视为非实体类。
如何构建这些类:
超类,也是你的 JSON 对象的POJO
@MappedSuperclass
public class BaseSector implements Serializable {
private String id;
private String name;
private String parentId;
}
实体类:
@Entity
@Table(name = "sector")
public class Sector extends BaseSector {
@Column(length = 36)
private String id;
@Column(length = 40)
private String name;
@Column(length = 36)
private String parentId;
// ... 其他一系列字段
}
你还可以在实体类(Sector)中覆盖超类(BaseSector)中的一些属性。你需要使用
@AttributeOverride // 用于单个属性
@AttributeOverrides // 用于覆盖多个属性
英文:
A TRANSIENT field tells your ENTITY class that this particular field should not be persisted in the DB. @Transient
annotation is used to ignore a field to not persist in database in JPA, where as transient
key word used to ignore a field from serialization. The field annotated with @Transient
still can be serialized, but the field declared with transient
keyword not to be persisted and not to be serialized.
A POJO can be extended by an ENTITY and vice-versa. This is stated in JPA specification.You can find more examples at the below links :
Link:1 : JPA Non-Entity SuperClass
You can achieve this by using an annotation : @javax.persistence.MappedSuperclass
It states : A superclass is treated as non-entity class if no mapping related annotations such as @Entity or @MappedSuperclass are used on the class level.
This means your superclass will be treated as a non-entity class here if you do not use the above annotations in your superclass.
How to Construct the classes :
SUPERCLASS which also a POJO for your JSON object
@MappedSuperclass
public class BaseSector implements Serializable {
private String id;
private String name;
private String parentId;
}
ENTITY class :
@Entity
@Table(name = "sector")
public class Sector extends BaseSector {
@Column(length = 36)
private String id;
@Column(length = 40)
private String name;
@Column(length = 36)
private String parentId;
// ... Bunch of other field
}
You can also override some property defined by BaseSector in your ENTITY - Sector
You need to use
@AttributeOverride // for single property
@AttributeOverrides // override more than one property
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论