0

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?

1 Answer 1

1

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

6
  • I thought about that way, but in this case for each log action I need to get correct Event instance (from db or cash), set it and only after that persist new LogEvent to database. That’s much more action, so I thought that maybe there’s a simpler way to do it May 8, 2018 at 14:16
  • Thre is no way to make reference for a record that doesn't exist. You must create it before pointing to it. This is referential integrity. May 8, 2018 at 14:21
  • Ok, and if records already exists (I can just add them to db directly), is it possible to give some string (or enum) in constructor, based on which will be created correct reference? May 8, 2018 at 14:39
  • Yes. It doesn't make sense to me, but yes. The point is: even if you implement some constructor String attribute, you must find by performing a query the existing record to create your LogEvent register. May 8, 2018 at 21:19
  • well, if I need to perform query, than I agree, it doesn't make sense. I thought that maybe there's some annotation like @ElementCollection and @CollectionTable but for single element. Anyway, thanks for help! I'll do as you adviced May 8, 2018 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.