Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3340212
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:34:53+00:00 2026-05-18T00:34:53+00:00

I have a fairly simple Many to One relationship between two classes: @Entity public

  • 0

I have a fairly simple Many to One relationship between two classes:

@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 steps(bytea) column is there. Am I doing something wrong in my mapping or do I just not understand how hibernate works?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T00:34:54+00:00Added an answer on May 18, 2026 at 12:34 am

    I suspect the problem is that you’re using a concrete HashSet instead of the Set interface. Try this (assuming it has an Id somewhere):

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

    Also note how I initialized the steps property. Let me quote the documentation about this:

    6.1. Persistent collections

    …

    Notice how the instance variable was
    initialized with an instance of
    HashSet. This is the best way to
    initialize collection valued
    properties of newly instantiated
    (non-persistent) instances. When you
    make the instance persistent, by
    calling persist() for example,
    Hibernate will actually replace the
    HashSet with an instance of
    Hibernate’s own implementation of Set.

    And make sure that:

    • both entities have an @Id property (the part you’re showing is not enough to confirm that).
    • Step is implementing equals/hashCode correctly (see the references below).

    References

    • Hibernate Core Reference Guide
      • 4.3. Implementing equals() and hashCode()
      • 6.1. Persistent collections

    Update: Can’t reproduce (I don’t have PostgreSQL installed by I don’t think it is that relevant). I used the following entities:

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

    And:

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

    Here is the generated 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
    

    I don’t even understand how you could use a HashSet in your OneToMany, Hibernate complained (as expected to be honest) when I tried:

    Caused by: org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.stackoverflow.q4083744.Schedule.steps
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly simple one-to-many type join in a MySQL query. In this
I have a fairly simple class that I want to save to SQL Server
I have a fairly simple web2py form with a few validators like, IS_FLOAT_IN_RANGE(...) on
I have a fairly simple question, however the answer seems to elude me. If
I have a fairly simple query: SELECT invoice_id WHERE employee_id = 'XXXX' AND customer_id
I have a fairly simple block of code. Sub Run(Name) on error resume next
I have a fairly simple python loop that calls a few functions, and writes
I have a fairly simple cell in a table with an inline style: <td
I have a fairly simple set of functionality for which I have multiple implementations,
I have a fairly simple Linq query (simplified code): dim x = From Product

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.