I've got two tables
Logs:
-------------------------------------------------------------------------------
| id (pk) | target | event_code (fk) |
-------------------------------------------------------------------------------
| 1 | System_Role | 1 |
--------------------------------------------------------------------------------
| 2 | System_RUle | 2 |
--------------------------------------------------------------------------------
| 1 | Internal_User | 3 |
--------------------------------------------------------------------------------
| 3 | External_User | 4 |
--------------------------------------------------------------------------------
Events:
-------------------------------------------------------
| id (pk) | event |
-------------------------------------------------------
| 1 | Role was added |
-------------------------------------------------------
| 2 | Rule was removed |
-------------------------------------------------------
| 1 | User was updated |
-------------------------------------------------------
| 3 | User password was reseted |
-------------------------------------------------------
Table logs has foreign key from field event_code to event's field id.
Entity:
@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;
}
}
I need to get field event from table Events and put it to variable event. And also I need to add new values correctly to my DB. How to do that?
What you need here, is to use a ManyToOne relationship with JPA, such as:
@ManyToOne
@JoinColumn(name="event_code", nullable=false)
private Event event;
This way you can easily manipulate the Event register corresponding to your LogEvent.
I strongly recommend you to learn about JPA relationships: https://www.tutorialspoint.com/jpa/jpa_entity_relationships.htm