1

I'm getting a weird result in session.save(). It is always returning 1. but the value in database is inserted correctly. Please suggest anything you found doubtful

Code

session = getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
userId = (Long)session.save(user);
transaction.commit();

User - Persistence

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "ID")
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

@Column(name = "USERNAME", length = 100)
public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

@Column(name = "PASSWORD", length = 100)
public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

@OneToOne(fetch = FetchType.EAGER)
@MapsId
@JoinColumn(name = "ROLEID")
public Role getRole() {
    return role;
}

public void setRole(Role role) {
    this.role = role;
}

@Column(name = "IMAGEURL", length = 2000)
public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

Version

Hibernate: 4.3.7 final

Spring: 4.1.2

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64

2 Answers2

2

Referring to this question Difference between @GeneratedValue and @GenericGenerator.

The problem may be with your @GeneratedValue.

So you can change

@GeneratedValue(strategy=GenerationType.SEQUENCE)

to

@GeneratedValue(generator="increment")

Note : You can refer this to assign your database sequence to @GeneratedValue

Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • Hi Thanks for the reply. Initially it was the same. I changed to this for trial and error.Again i tried now as you suggested, Unfortunately it's not working. – Viswanath Lekshmanan Dec 17 '14 at 11:47
  • So the ID value is getting inserted properly in your table? – Naman Gala Dec 17 '14 at 11:48
  • Yes, Its properly inserted in my table. But returning wrong value – Viswanath Lekshmanan Dec 17 '14 at 12:11
  • When you say its correctly inserting are the ID column values being incremented in the database.Is that column a primary key in table? – Balaji Dec 17 '14 at 12:33
  • Yes, It is. You can see that @Id there. It's auto increment – Viswanath Lekshmanan Dec 17 '14 at 12:38
  • There is a possibility that you define @Id at hibernate and still could have forgotten to define a primary key on your table. So the ID column values in database table User are incrementing on each save and the records are persisted but id value is not correctly returned by the save method? – Balaji Dec 17 '14 at 12:48
  • Yes, as said by @Balaji, check your table structure.. Is primary key is defined? – Naman Gala Dec 17 '14 at 12:52
  • @NamanGala and Balaji The table structure was correct the problem was the onetoOne mapping it was manyToOne So it was restricted to select. Thanks for the help. – Viswanath Lekshmanan Dec 18 '14 at 04:06
1

From the code I see that your generation strategy is Sequence. But you missed to map the sequence to the annotation.

https://docs.oracle.com/javaee/5/api/javax/persistence/GeneratedValue.html

@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="YOUR_SEQ")
@Column(name = "ID")
public Long getId() {
    return id;
}

Else if you are using a custom generator then you should have mapped as follows:

@Id
@GeneratedValue(generator= "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "ID")
public Long getId() {
    return id;
}
Balaji
  • 1,009
  • 7
  • 21