I've been scratching my head on this one and I'm not sure Hibernate won't play nice with MySQL. I am getting the following error:
Code:
java.sql.SQLException: Field 'member_id' doesn't have a default value
... (and many more parts of the stack trace)
I've tried:
- @GeneratedValue(strategy = AUTO)
- @GeneratedValue(strategy = ENTITY)
- @GeneratedValue
- [no reference to GeneratedValue at all]
But I still get:
Code:
java.sql.SQLException: Field 'member_id' doesn't have a default value
The MySQL DB has the following table:
Code:
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| member_id | int(10) | NO | PRI | NULL | auto_increment |
| lastname | varchar(20) | NO | | | |
| firstname | varchar(20) | NO | | | |
| onlinename | varchar(20) | NO | | | |
| password | varchar(15) | NO | | | |
| url | varchar(50) | YES | | NULL | |
| membersince | date | NO | | | |
| email | varchar(40) | NO | | | |
+-----------------+-------------+------+-----+---------+----------------+
The Member class representing this table is:
Code:
@Entity
@Name("member")
@Table(name="member")
public class Member implements Serializable {
private static final long serialVersionUID = 1881413500711441951L;
private int memberID;
private String lastName;
private String firstName;
private String onlineName;
private String password;
private String emailAddress;
private String url;
private Date memberSince;
private OnlineAccount onlineAccount;
private MemberAddress memberAddress;
...
@Id @GeneratedValue(strategy = AUTO)
@Column(name = "member_id")
public int getMemberID() {
return memberID;
}
@OneToOne(optional = true, cascade = ALL, fetch = EAGER)
@JoinColumn(name="memberaddress_id")
public MemberAddress getMemberAddress()
{
return memberAddress;
}
@OneToOne(optional = false, cascade = ALL)
@JoinColumn(name="onlineaccount_id")
public OnlineAccount getOnlineAccount()
{
return onlineAccount;
}
}
Member has two one-to-one relationships with the following weak entities:
MemberAddress.java (which is optional - see below annotation):
Code:
@Entity
@Name("memberaddress")
@Table(name="memberaddress")
public class MemberAddress {
private String mailingAddress;
private String stateOrProvince;
private String zipOrpostalCode;
private long memberAddressID;
private Member owner;
public MemberAddress(Member owner)
{
this.owner = owner;
}
public void setOwner(Member owner)
{
this.owner = owner;
}
@Id @GeneratedValue(strategy = AUTO)
@Column(name = "memberaddress_id")
public long getMemberAddressID() {
return memberAddressID;
}
}
And finally the OnlineAccount.java class (which is not optional):
Code:
@Entity
@Name("onlineaccount")
@Table(name="onlineaccount")
public class OnlineAccount
{
private long onlineAccountID;
private String companyName;
private String accountNumber;
private Member owner;
public void setOwner(Member owner)
{
this.owner = owner;
}
@OneToOne(mappedBy = "onlineAccount")
public Member getOwner()
{
return owner;
}
@Id @GeneratedValue(strategy = AUTO)
@Column(name = "onlineaccount_id")
public long getOnlineAccountID() {
return onlineAccountID;
}
}
Not sure why Hibernate won't pick up on the fact that I want MySQL to generate this since that's what the annotations are suggesting. Anyone have an idea what I'm doing wrong?
Btw, I'm using Seam 2.02, Hibernate 3.2.4.sp1, Tomcat 6, MySQL 5. Any help appreciated.