@Formula does not hibernate with object

I have an enum with a small status value

NEW, REVIEWD, PUBLISHED, CONNECTED, UPDATED, SPAM, DUPLICATE, IRRELEVANT, UNPUBLISHED

I don't want to use them as enumerables, so one object is created for that. For convenience, I want to store a column in an entity to initialize a status from an enum and convert that enum to a status object object. for this..

I have two objects. I want to pass a column with a value from another object.

Basically, I want to initialize an object using a formula.

Entities

@Entity
@Table(name = "event_status")
public class EventStatus {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="eventStatusId")
    private Integer eventStatusId;

    @Enumerated(EnumType.STRING)
    @Column(unique = true,name="eventStatusType")
    private EventStatusType eventStatusType;

    public EventStatus() {
        this(EventStatusType.NEW);
    }

    public EventStatus(EventStatusType eventStatusType) {
        super();
        this.eventStatusType = eventStatusType;
    }

    public Integer getEventStatusId() {
        return eventStatusId;
    }

    public EventStatusType getEventStatusType() {
        return eventStatusType;
    }

    public void setEventStatusId(Integer eventStatusId) {
        this.eventStatusId = eventStatusId;
    }

    public void setEventStatusType(EventStatusType eventStatusType) {
        this.eventStatusType = eventStatusType;
    }
}

      

I have another object in which I am referencing the object of this object

@Entity
@Table(name = "event_")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Event implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "id_")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Transient
    public EventStatusType eventStatusType = EventStatusType.NEW;

    @ManyToOne(fetch = FetchType.EAGER, targetEntity = EventStatus.class)
    @Formula("select * from event_status where eventStatusId= 1")
    private EventStatus status;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public EventStatus getStatus() {
        System.out.println("Event.getStatus() " + status);
        return status;
    }

    public void setStatus(EventStatus status) {
        System.out.println("Event.setStatus()");
        this.status = status;
    }
}

      

This does not throw any exceptions, but it does not initialize this value. Is it possible to initialize this EntityStatus with eventStatusType value on Event entity

+3


source to share


1 answer


I would like to explain that based on the documentation :

5.1.4.1.5. Formula

Sometimes you want the database to do some calculations for you and not in the JVM, you can create some kind of virtual column as well. You can use SQL snippet (aka formula) instead of mapping property to column. This type of property is read-only (its value is calculated from a fragment of a formula).

@Formula("obj_length * obj_height * obj_width")
public long getObjectVolume()

      

A chunk of SQL can be as complex as you want and even include subqueries.

...



5.1.7.1. Using a foreign key or association table

...



Note

You can use SQL snippet to simulate a physical join column using annotations @JoinColumnOrFormula

/ @JoinColumnOrformulas

(just like you can use SQL snippet to simulate a properties column using annotations @Formula

).

@Entity
public class Ticket implements Serializable {
    @ManyToOne
    @JoinColumnOrFormula(formula="(firstname + ' ' + lastname)")
    public Person getOwner() {
        return person;
    }
    ...
} 

      

In addition, we must use , since such a display is not editable insertable = false, updatable = false

+3


source







All Articles