I'm pretty newbie to Hibernate and I'm having a hard time trying to figure out how to solve the next issue:
I have a class, named User that looks like this:
@Entity
@Table(name = "users")
public class User implements Serializable {
@Id
@Column(name = "id_user")
int id_user;
@Column(name = "name")
String name;
@Column(name = "surname")
String surname;
@OneToOne
@JoinColumn(name = "city", referencedColumnName = "id_city", nullable = false, insertable = false, updatable = false)
City city;
And the related class:
@Entity
@Table(name = "cities")
public class City implements Serializable {
@Id
@Column(name = "id_city")
int id_city;
@Column(name = "name")
String name;
As you can think, the table "cities" contains all the cities of a country and they're not going to be added or deleted, they're there just to be referenced. I would like the id of the city to be saved on the users table (as a smallint(6) value), but that field remains null when I save the user. All the other fields are correctly saved and I have checked that while execution, the property city of the user is correctly set with a city. The related code in my servlet is:
active = new User();
active.setName(name);
City arrival = (City) session.get(City.class, "1");
active.setArrival(arrival);
session.save(active);
session.getTransaction().commit();
I don't know if this is the best way to do it. I have set an OneToOne unidirectional relation between the classes. Maybe it's wrong. I don't know, everything seems to work fine but that property. I have checked tons of topics and this one Hibernate: how do I map one to one where B is a property of A? is kind of similar to my issue but I can't make the solution there work for me.
Any help would be appreciated.