Reputation: 8259
I have two classes, Invite
and Event
with a many-to-one mapping: one event has many invites. Here are the relevant snippets of code/SQL with Hibernate annotations:
db.Events
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| event_id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
db.Invites
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| invite_id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| event_fk | int(11) | NO | MUL | NULL | |
+-----------+--------------+------+-----+---------+----------------+
DbInvite.java
@ManyToOne
private DbEvent event;
public DbInvite() {
}
public DbInvite(String name, DbEvent event) {
this.name = name;
this.event = event;
}
// getters and setters
DbEvent.java
@OneToMany(mappedBy = "event")
private Set<DbInvite> invites;
public DbEvent() {
}
public DbEvent(String name) {
this.name = name;
}
// getters and setters
Finally, I--and this is where most Hibernate examples appear to stop--I create my instances in the following way:
DbEvent dbEvent = new DbEvent(name);
DbInvite dbInvite = new DbInvite(name, dbEvent);
The error I receive is:
org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Unknown column 'event_event_id' in 'field list'
Where am I going wrong? Where does event_event_id
come from when the primary key is clearly event_id
?
Upvotes: 0
Views: 353
Reputation: 23552
You need to specify join column for many-to-one, otherwise defaults are assumed by JPA/Hibernate.
Upvotes: 1
Reputation: 220
When you do:
DbEvent dbEvent = new DbEvent(name);
//the id is setted in when persist into the db
dbEvent = someMethodThatPersistInDb(dbEvent);
//now the object have the id created in the db now insert the other one
DbInvite dbInvite = new DbInvite(name, dbEvent);
someMethodThatPersistInDb(dbInvite);
i hope that can help you, maybe you can post the rest of the mapping class to check the code.
Upvotes: 0