我有两张桌子
日志:
-------------------------------------------------------------------------------
| id (pk) | target | event_code (fk) |
-------------------------------------------------------------------------------
| 1 | System_Role | 1 |
--------------------------------------------------------------------------------
| 2 | System_RUle | 2 |
--------------------------------------------------------------------------------
| 1 | Internal_User | 3 |
--------------------------------------------------------------------------------
| 3 | External_User | 4 |
--------------------------------------------------------------------------------
活动:
-------------------------------------------------------
| id (pk) | event |
-------------------------------------------------------
| 1 | Role was added |
-------------------------------------------------------
| 2 | Rule was removed |
-------------------------------------------------------
| 1 | User was updated |
-------------------------------------------------------
| 3 | User password was reseted |
-------------------------------------------------------
表日志具有从字段event_code到event&#39的字段id的外键。
实体:
@Entity
@Table(name = "logs")
public class LogEvent implements Serializable{
private static final long serialVersionUID = 1;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 150)
@Column(name = "target")
private String target;
//????
private String event;
public LogEvent(String target, String event) {
this.target = target;
this.event = event;
}
}
我需要从表事件中获取字段事件并将其放入变量事件。而且我还需要在我的数据库中正确添加新值。怎么做?
答案 0 :(得分:1)
这里需要的是与JPA使用ManyToOne关系,例如:
@ManyToOne
@JoinColumn(name="event_code", nullable=false)
private Event event;
通过这种方式,您可以轻松地操作与LogEvent对应的事件寄存器。
我强烈建议您了解JPA关系: https://www.tutorialspoint.com/jpa/jpa_entity_relationships.htm