简体   繁体   中英

Hibernate @Any annotation usage

I have one entity called Change where I need log changes in database like inserting, updating or deleting rows.

So my Change table contains some data and now I would like to add foreign key to record changes in another table, but I have different tables. For example I have Weather table, Group table,... So I have done some searching and I have found a little bit about @Any annotation. So I added some columns to my Change entity:

@Entity
@Table(name = "CHANGE")
public class Change {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "CHANGE_ID")
    private int changeId;
...
@Any(metaColumn = @Column(name = "RECORD_TABLE"))
@AnyMetaDef(idType = "int", metaType = "string",
metaValues = {
@MetaValue(targetEntity = Weather.class, value = "WEATHER"),
@MetaValue(targetEntity = Group.class, value = "GROUP"),
...
})
@JoinColumn(name="recordID")
private Object record;

@ManyToOne
@JoinColumn(name = "USER_ID")
private User user;

public Object getRecord() {
    return record;
}
public void setRecord(Object record) {
    this.record = record;
}
...

And my stupid question is: How can I insert data into database (like foreign ID and class name) and how could I retrieve them?

Please go through this Link

You should care about your entity relationship (1-1 or 1-M or MM)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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