0

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.

Community
  • 1
  • 1
  • I assume that there are multiple users from the same country so one to one is not correct. What you need there is a ManyToOne in the User class. – mh-dev May 18 '15 at 08:17
  • I've set the relation to OneToOne because although it is true that many users can come from the same town, I don't care about that way of the relation. I only need to know that there is a town for every user. I think that is what unidirectional means to, isn't it? – Kaliotto May 18 '15 at 17:35

1 Answers1

0

You need to remove the 'insertable = false' and 'updatable = false' attributes. These would make sense if the association was bi-directional and was being managed from the other side (City) which is not the case here.

Puneet Dev
  • 21
  • 4
  • I don't know if it is only beacuse of that, but besides of removing the nullable, insertable and updatable properties, I've reorganized all the code. I mean, I had a lot of comments between some annotations and the declaration of the property. A mess like this: `@OneToOne(cascade = CascadeType.ALL) //@ManyToOne //@OneToMany @JoinColumn(name = "city", referencedColumnName = "id_city")//, nullable = false, insertable = false, updatable = false) //@PrimaryKeyJoinColumn City city;` It seems to work just fine now without all the unnecesary comments. Thanks! – Kaliotto May 18 '15 at 18:01