Hibernate annotation of a multi-valued mapping generating an odd pattern

I have a pretty simple relationship between two classes between two:

@Entity
public class Schedule
 implements java.io.Serializable {

    private String scheduleName;
    private HashSet<Step> steps;

    @OneToMany(mappedBy="schedule", cascade=CascadeType.ALL, 
        fetch=FetchType.EAGER)
    public HashSet<Step> getSteps() {
       return steps;
    }
}

@Entity
public class Step implements java.io.Serializable {
   private Long id; 
   private String duration;
   private String stepType;
   private Schedule schedule;

   @ManyToOne(fetch=FetchType.LAZY)
   public Schedule getSchedule() {
     return schedule;
   }

   @Id
   @GeneratedValue
   public Long getId() {
       return id;
   }

}

Hibernate generates the following tables (in Postgres)

              Table "public.schedule"
    Column    |          Type          | Modifiers 
--------------+------------------------+-----------
 uuid         | character varying(255) | not null
 version      | integer                | 
 schedulename | character varying(255) | 
 steps        | bytea                  | 


                Table "public.step"
    Column     |          Type          | Modifiers 
---------------+------------------------+-----------
 id            | bigint                 | not null
 duration      | character varying(255) | 
 steptype      | character varying(255) | 
 temperature   | numeric(19,2)          | 
 schedule_uuid | character varying(255) | 

The step table is what I expect, but I don’t understand why the step column is (bytea). Am I doing something wrong in my comparison or just don’t understand how sleep mode works?

+3
source share
1 answer

I suspect the problem is that you are using a specific HashSetinstead of an interface Set. Try this (if it has Idsomewhere):

@Entity
public class Schedule implements java.io.Serializable {

    private String scheduleName;
    private Set<Step> steps = new HashSet<Step>();

    @OneToMany(mappedBy="schedule", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    public Set<Step> getSteps() {
       return steps;
    }

    // other properties, getters, setters
}

, . :

6.1.

...

, HashSet. () . , persist(), , Hibernate HashSet Hibernate Set.

, :

  • @Id (, , ).
  • Step equals/hashCode (. ).


: ( PostgreSQL, , , ). :

@Entity
public class Step implements java.io.Serializable {
    private Long id;
    private String duration;
    private String stepType;
    private Schedule schedule;

    @ManyToOne(fetch = FetchType.LAZY)
    public Schedule getSchedule() { return schedule; }

    @Id @GeneratedValue
    public Long getId() { return id; }

    // getters, setters, equals, hashCode
}

@Entity
public class Schedule implements java.io.Serializable {
    private Long id;
    private String scheduleName;
    private Set<Step> steps = new HashSet<Step>();

    @OneToMany(mappedBy = "schedule", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    public Set<Step> getSteps() { return steps; }

    @Id @GeneratedValue
    public Long getId() { return id; }

    // getters, setters
}

DDL:

create table Schedule (
    id bigint generated by default as identity (start with 1),
    scheduleName varchar(255),
    primary key (id)
)

create table Step (
    id bigint generated by default as identity (start with 1),
    duration varchar(255),
    stepType varchar(255),
    schedule_id bigint,
    primary key (id)
)

alter table Step 
    add constraint FK277AEC7B775928 
    foreign key (schedule_id) 
    references Schedule

, HashSet OneToMany, Hibernate ( , ), :

Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.stackoverflow.q4083744.Schedule.steps
+6

Source: https://habr.com/ru/post/1772769/


All Articles