简体   繁体   English

使用@Transient Annotation进行持久性存储

[英]Using @Transient Annotation for persistance storage

I have an object which contains multiple attributes. 我有一个包含多个属性的对象。 one of the attribute of my object is another object containing ArrayList. 我对象的属性之一是另一个包含ArrayList的对象。 Hierarchy is as follow. 层次结构如下。

Object - Organization
(
String orgId
String orgName
String parentOrgName

Organization.Workers workers  //Object of Worker list...

)

I want to store Organization object in imdb (in memory database). 我想将组织对象存储在imdb中(在内存数据库中)。 I am using Hibernate session for database connection. 我正在使用Hibernate会话进行数据库连接。

I have already set Worker object as Transient like this. 我已经将Worker对象设置为Transient这样。

@Transient
protected Organization.Workers workers;

When I retrieve Organization, every time I get this worker object as null. 当我检索组织时,每次我将此工作对象获取为null时。 Kindly guide me about use of @transient annotation and also tell about how to store nested objects in imdb using hibernate sessions in java spring. 请指导我有关@transient批注的使用,并告诉我如何使用java spring中的休眠会话将嵌套对象存储在imdb中。

@Transient annotation is used to tell JPA not to store field in the database. @Transient批注用于告诉JPA不要将字段存储在数据库中。 Also, whenever you are dealing with composition, figure out the relationship they have in between them. 另外,无论何时处理合成,都要弄清楚它们之间的关系。

In above example, the relationship seems to be one-to-many as one organization can have many workers but one worker can work in only one organization at a time. 在上面的示例中,这种关系似乎是一对多的,因为一个组织可以有很多工人,但是一个工人一次只能在一个组织中工作。

You will have to tell JPA about this relationship using @OneToMany annotation. 您将必须使用@OneToMany注释将这种关系告知JPA。

Checkout this official link of Hibernate on how to use it. 查看Hibernate的官方链接以了解如何使用它。

The @Transient annotation is used when you need that specific field for certain work but are not willing that field to store in the database. @Transient批注用于需要特定字段进行某些工作但不愿意将该字段存储在数据库中时使用。

Here as your question is stated, there are organization and workers, so the relationship is @OneToMany relationship. 正如您的问题所述,这里有组织和工作人员,所以关系是@OneToMany关系。

In Organization Class: 在组织类中:

@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="organization")
private List<Workers> workers = new ArrayList<>();

In Workers Class: 在工人阶级中:

@ManyToOne
@JoinColumn(name="id", unique=true)
private Organization organization;

Here, 1 organization can have many workers and one worker can only work on 1 organization. 在这里,一个组织可以有很多工人,而一个工人只能在一个组织上工作。 there you have it. 你有它。 :D :d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM