-1

i try to use save() method, but the database schema doesn't have incremented value for Id, so it's giving Exception: ids for this class must be manually assigned before calling save()

so i want to do it, how can i do this?

user2888996
  • 417
  • 8
  • 20
  • Question is vague and confusing. Not clear whether the question is about Hibernate schema generation, persisting entities or using Hibernate with databases which do not support auto-incrementing PK column. – Alan Hay Oct 24 '13 at 08:53

2 Answers2

0

You can use @GeneratedValue annotation and specify the GenerationType IDENTITY.

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public Long getId() { ... } 

Read more at http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

Sachin Gorade
  • 1,427
  • 12
  • 32
  • yes, it will because when you use IDENTITY then you have to manually set the value of id field. – Sachin Gorade Oct 24 '13 at 06:28
  • what i want is:without Annotation we use Save() method that time it returns the Id right? same way i want using annotations..and my column should not be auto_increment – user2888996 Oct 24 '13 at 06:31
  • As you said your column should not auto_increment but you want to use save method, so according to http://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html#save(java.lang.Object) this method returns object with auto generated identifier or in case of IDENTITY(or assigned) generator will use existing ID. – Sachin Gorade Oct 24 '13 at 06:36
  • Yes i understand but, look i am creating this column using hbm.xml Here column is created by Hibernate itself(without auto_increment) and i am using Save(), so it is returning me id..everything is fine. But, when i use the same schema with Annotation like : !@Id !@GeneratedValue @Column(name = "msgId") @Type(type = "long") public Long msg; public String msgID; it's giving Exception: id is null – user2888996 Oct 24 '13 at 06:40
  • Read this link - http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/mapping.html . It explains about generator values. – Sachin Gorade Oct 24 '13 at 06:45
  • Because you have not specified a strategy. @GeneratedValue(strategy = GenerationType.......) – Alan Hay Oct 24 '13 at 07:26
  • @AlanHay by-default it takes incremented – user2888996 Oct 24 '13 at 07:27
  • I am not exactly sure what your question is. Is the relevant column in the database set to auto-increment or not? – Alan Hay Oct 24 '13 at 07:30
  • look i am creating this column using hbm.xml Here column is created by Hibernate itself(without auto_increment) and i am using Save(), so it is returning me id..everything is fine. But, when i use the same schema with Annotation like : !@Id !@GeneratedValue @Column(name = "msgId") @Type(type = "long") public Long msg; public String msgID; it's giving Exception: id is null – user2888996 Oct 24 '13 at 07:31
  • Using hbm.xml: +---------+------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+------------+------+-----+---------+-------+ | msgId | bigint(20) | NO | PRI | NULL | | | message | longtext | YES | | NULL | | +---------+------------+------+-----+---------+-------+ – user2888996 Oct 24 '13 at 07:33
  • Using Annotations: +---------+------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+------------+------+-----+---------+----------------+ | msgId | bigint(20) | NO | PRI | NULL | auto_increment | | message | longtext | YES | | NULL | | +---------+------------+------+-----+---------+----------------+ – user2888996 Oct 24 '13 at 07:34
  • SO, when i use save method with old schema(hbm.xml one) then it says msgId is null – user2888996 Oct 24 '13 at 07:35
  • This post could be useful - http://stackoverflow.com/questions/6776308/hibernate-generator-increment-confict-with-table-auto-increment – Sachin Gorade Oct 24 '13 at 07:43
  • So what exactly are you asking? Why Hibernate DDL did not create the table with auto-increment for your table when using hbm.xml rather than annotations? – Alan Hay Oct 24 '13 at 07:45
  • @code13..i get that, but still i don't understand how can i save method work using annotation without assigning the id's values manually – user2888996 Oct 24 '13 at 08:27
  • In that case you have to use @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; and check your hibernate dialect is configured correctly. – Sachin Gorade Oct 24 '13 at 08:35
  • @code13 i done this, but still not happening. Exception: Field 'msgId' doesn't have a default value – user2888996 Oct 24 '13 at 08:40
  • what database you are using? and please your question with hibernate dialect configuration so that others can also give some directions. – Sachin Gorade Oct 24 '13 at 08:47
  • Sorry, please revise your original question as I am not sure what the issue is. As far as I can determine you are asking why save does not work when using the increment strategy on a table without an auto-increment PK field. Well the obvious answer to that is to update your database table to have an auto-increment PK. – Alan Hay Oct 24 '13 at 08:50
  • i am using MysqlDialect, and @AlanHay i want save() method to work with old schema that doesn't have auro_increment column – user2888996 Oct 24 '13 at 09:04
  • Well as I pointed out in my original answer if you do not have an auto-increment column then you will need to either (a) add one or (b) use another ID generation strategy. So please read the link and choose a strategy based on your requirement. – Alan Hay Oct 24 '13 at 09:09
  • @AlanHay Okay i'll do that, one more thing,i define id in hibernate.hbl.xml as "increment" but in database table column doesn'y have auto_increment..why so?? – user2888996 Oct 24 '13 at 10:41
0

There are various other options available for key generation.

See here:

How to choose the id generation strategy when using JPA and Hibernate

Community
  • 1
  • 1
Alan Hay
  • 22,665
  • 4
  • 56
  • 110