Hi folks,
I am having a problem with the following relationships (summarized).
Code:
class Mailbox
{
@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@OrderBy(value="date desc")
private Collection<Message> sent;
@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@OrderBy(value="date desc")
private Collection<Message> drafts;
@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL)
@OrderBy(value="date desc")
private Collection<Message> received;
}
Code:
class Message
{
@Column(nullable=false,length=3000
,columnDefinition="VARCHAR(3000) CHARACTER SET UTF8")
String body;
@Column(nullable=false)
@Temporal(value=TemporalType.TIMESTAMP)
private Date date;
}
Using hibernate 3.1cr1, annotations beta9, entitymanager beta7 and mysql 5.0.11.
Hibernate generates the following join table with appropriate foreign keys (trying to keep the post small, so I cut them out).
Code:
create table dtg_mailbox_dtg_message (
dtg_mailbox_id varchar(32) not null,
received_id varchar(32) not null,
drafts_id varchar(32) not null,
sent_id varchar(32) not null,
unique (drafts_id),
unique (received_id),
unique (sent_id)
) type=InnoDB;
Adding a message to drafts creates the following SQL.
Code:
insert
into
dtg_mailbox_dtg_message
(dtg_mailbox_id, drafts_id)
values
(?, ?)
09:55:41,843 DEBUG StringType:80 - binding '402882960a830363010a830371580001' to parameter: 1
09:55:41,843 DEBUG StringType:80 - binding '402882960a830363010a830371770003' to parameter: 2
Which (you may have guessed the next bit) causes the following error.
Code:
09:55:41,859 WARN JDBCExceptionReporter:71 - SQL Error: 1364, SQLState: HY000
09:55:41,859 ERROR JDBCExceptionReporter:72 - Field 'received_id' doesn't have a default value
This has a bug feel about it, since that join table can never be correct. I could probably fix it with a @JoinColumn annotation, or by having inverse mappings from the target entity, but I still can't shake the feeling that this should have worked and generated correct ddl.
Any pointers for the bemused?
Many thanks.