2015-06-14 16 views
0

我有一些實體,我需要某些字段不能被保留,有些字段不能被序列化。在同一個文件中對Gson和Hibernate使用瞬態

我在一些字段上使用@Transient,但是當我想爲Gson標記瞬態時。問題在於,休眠選取它並且也不會保留它,因爲它也是Hibernate中的關鍵字。

我使用Hibernate,JPA-2.1-API javax.persistence.Transient

我試圖阻止addresses被序列化和getDefaultAddress不應該被保存。

代碼:

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

    @OneToMany(mappedBy="business") 
    private transient List<Phone> addresses; 

    @Transient 
    public Phone getDefaultPhone() { 
     return phones.get(0); 
    } 
} 

任何解決方案?

回答

0

您可以使用@Expose

@Expose(serialize = false) 
@OneToMany(cascade=CascadeType.ALL, mappedBy="business") 
private List<Address> addresses; 

爲了能夠與此註釋工作,

final GsonBuilder builder = new GsonBuilder(); 
builder.excludeFieldsWithoutExposeAnnotation(); 
final Gson gson = builder.create(); 

來源:http://www.javacreed.com/gson-annotations-example/

相關問題