如何从另一个 table 获取单个值作为字符串

How to get single value from another table as String

我有两个 tables

日志:

-------------------------------------------------------------------------------
|   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 |
-------------------------------------------------------

Table 日志具有从字段 event_code 到事件字段 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;
    }        
}

我需要从 table 事件中获取字段事件并将其放入变量事件中。而且我还需要将新值正确添加到我的数据库中。怎么做?

这里你需要的是使用 JPA 的 ManyToOne 关系,例如:

@ManyToOne 
@JoinColumn(name="event_code", nullable=false)
private Event event;

这样你就可以方便的操作你的LogEvent对应的Event寄存器了

我强烈建议您了解 JPA 关系: https://www.tutorialspoint.com/jpa/jpa_entity_relationships.htm