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?
I suspect the problem is that you’re using a concrete
HashSetinstead of theSetinterface. Try this (assuming it has anIdsomewhere):Also note how I initialized the steps property. Let me quote the documentation about this:
And make sure that:
@Idproperty (the part you’re showing is not enough to confirm that).Stepis implementingequals/hashCodecorrectly (see the references below).References
Update: Can’t reproduce (I don’t have PostgreSQL installed by I don’t think it is that relevant). I used the following entities:
And:
Here is the generated DDL:
I don’t even understand how you could use a
HashSetin yourOneToMany, Hibernate complained (as expected to be honest) when I tried: