sir,your explanation is very clean and can understand carefully what you can say.thank you
@TheGuroguro127 жыл бұрын
Thank you very much. for those who watching this since hilo got deprecated, use sequence-gen instead of hilo.
@bhaskaryerraguntla23689 жыл бұрын
Thank you so much Koushik. These are very good tutorials for beginners.
@mustafazayed83087 ай бұрын
Hi sir, thx for this amazing course.🙂 - I think @CollectionId has a different implementation in recent releases of hibernate as it's no longer has type attribute nor @GenericGenetaror. - Why do we even want to add an id for Address table? Isn't it supposed to be a value type that doesn't have its id?
@pareek.himanshu4 жыл бұрын
If you are using MySQL database use the following syntax: @GenericGenerator(name = "sequence_gen", strategy = "sequence") @CollectionId(columns = @Column (name = "ADDRESS_ID"), type = @Type(type = "long"), generator = "sequence_gen") because 'hilo' is not supported anymore and in MySQL table name can not contain a hyphen(-).
@mamlazz27 жыл бұрын
I find it that the best to use in situation like this is increment strategy, because than the IDs will start from 1...if we use sequence strategy, the ids for ADDRESS_ID will not start from 1, it will generate the id for users...so if we have two users the ADRESS_ID will start form 3..With strategy=increment the IDs will start from 1
@Java.Brains13 жыл бұрын
@cssd1983j2ee Not sure if I understand your question right. As long as you use annotations, Hibernate assumes all fields need to be persisted (unless marked as @ Transient). That's the default behavior. It creates column names same as field names. For Collections, it also combines entity names to make sure the resulting column names do not repeat. But it saves all columns by default (including those in collections and related value types).
@Tomy97xbox7 жыл бұрын
Use @GeneratedValue(strategy = GenerationType.IDENTITY) instead of @GeneratedValue(strategy = GenerationType.AUTO) to ensure Primary Keys start at 1. If you use Auto, then all tables share the same auto increment value instead of each having their own value beginning at 1
@pradeepreddy94059 жыл бұрын
Seems, hi-lo is not supported, and "sequence" worked for me: @ElementCollection @GenericGenerator(name = "sequence", strategy = "sequence") @GeneratedValue(generator = "sequence", strategy=GenerationType.SEQUENCE) @CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "sequence", type = @Type(type = "long") ) @JoinTable(name="USER_ADDRESS", joinColumns=@JoinColumn(name="USER_ID")) //private Set addrSet= new HashSet(); private Collection listOfAddresses = new ArrayList();
@gillianbc9 жыл бұрын
+Pradeep Reddy I have the same problem - I'm using hibernate 5. I tried sequence but I get a duplicate primary key "Duplicate entry '1' for key 'PRIMARY'"
@gillianbc9 жыл бұрын
+gillian bc Actually, that was caused by a FK constraint on the database - a table had failed to drop. All fine once I dropped tables manually and started from a clean sheet again
@chenxiaotian26569 жыл бұрын
+Pradeep Reddy why my first primary key is 2 ,not 1
@chenxiaotian26569 жыл бұрын
+Pradeep Reddy problem solved.I already used generatedvalue early in the same class to generate primary key in another table. Can i still use twice generatedvalue and also start at 1?
@chenxiaotian26569 жыл бұрын
+Pradeep Reddy problem solved.I use type.table instead of sequence
@bkg_random5 жыл бұрын
I could not get the @JoinTable annotation to work and I was getting errors related to my primary key. In case anyone else is having the same issue, the fix for me was adding referencedColumnName as part of the syntax. In case it was: @JoinColumn(name="USER_ID", referencedColumnName="User_Id")) And again I can’t be grateful enough to koushik.
@tonythuanho807711 жыл бұрын
In genericGenerator, I use strategy =increment. I can solve the problem. @GenericGenerator(name = "hilo-gen", strategy = "increment")
@itsagame919 жыл бұрын
Thanks for your wonderful tutorials, Why can't we define a int field in Address class and annotate it with @Id annotation to make it primary key of that table?
@ShivaKumar-ns2oc8 жыл бұрын
+mayank chawla I tried the same, it hasn't worked. sequence-gen is working good. Kaushik, could you put some light into this?
@saurabhbhattacharya12175 жыл бұрын
This is probably because Address is not listed as an Entity in the hibernate config file. And we should not add that in the xml file because this is an dependent entity and unless and until the parent entity (e.g. UserDetails in these tutorials) is created there is no point of creating this entity (add this table) to your database.
@btharunreddy9 жыл бұрын
Hi Kaushik, I could see that there is an primary key that is getting added by default to Address table zip column. I could not understand why and how to remove it.
@borisjurosevic17711 жыл бұрын
great tutorial sir
@amsfuy11 жыл бұрын
Did Hibernate generate an additional table called 'hibernate_unique_key' for you guys as well?
@kashyapsumeet11 жыл бұрын
Thanks for the tutorial. I have a problem on MySql, the generator gives random value for new addresses added to the table. Eg. for the first insert of 2 addresses, the address_id is 1 and 2. Next time if i run the code it added address id, 32768 and 32769. Some other random values for the third run. Any solution?
@techdesigndedics9 жыл бұрын
Hey I am getting a unique constraint violation problem. I have an entity as Order and Value type as Product using Collections Set. It is not able to insert dublicate order id values. the primary key is not getting linked with the foreign key.
@kapoeiradobrasil9 жыл бұрын
+dheeraj agarwal It happened to me, first make sure that before you execute the code, there are no tables on your database and restart the db service after that try to run it again, I was using Eclipse and Mysql. This kind of error is produced when you run the code twice without noticing or terminating the first instance that you were executing. Hope it helped someway
@khoanguyen-ru5sp10 жыл бұрын
Can anyone plz help me? USER_ID in the sub table keep incrementing every time I run the application. This does not match with the actual USER_ID in main table.
@karanac35825 жыл бұрын
Hi sir , i am getting the following error "Exception in thread "main" org.hibernate.AnnotationException: No identifier specified for entity:" Thanks in advance.
@hashiroxx11 жыл бұрын
Hi Sir When I run the class in database new table created call hibernate_unique_key. So is it ok? or what is the purpose of that table? Waiting for your reply. Any way thanks for such good tutorials..
@pushpendradwivedi702810 жыл бұрын
Hi Sir, Nice explanation.. I am getting one error while trying the code of this video. Error is: Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: USER_DETAILS, for columns: [org.hibernate.mapping.Column(listOfAddresses)]. Please help what wrong I have done or what needs to be changed. I putted the same code as in the video. I am using oracle for the database.
@skazis8712 жыл бұрын
when I put another entry in the user_address table I don't get address_id's as 3 and 4 but as 32678 and 32679. what's the problem? I tried to set CollectionId's type as int but it didn't work.
@prativkumarhumnabad711511 жыл бұрын
thanks sir for providing woderfull videos.
@shilpag532911 жыл бұрын
Hi Sir, Thank you very much for your explanation. I am trying to do this generating Id from hibernate annotaion as like in 13 th tutorial. But i am getting error like org.hibernate.MappingException: Could not determine type for: Long. Can you tell me what wrong i did? thanks shilpa
@Vikas-uc4kw8 жыл бұрын
@GenericGenerator(name = "hilo-gen", strategy = "hilo") @CollectionId(columns = { @Column(name="Address_id") }, generator = "hilo-gen", type = @Type(type="long")) instead of this we can use below anotation from javax.persistence package. @OrderColumn(name = "ADDRESS_INDEX")
@asashish9055 жыл бұрын
Use strategy as 'increment' in case of MySQL.
@Iwitrag5 жыл бұрын
So this means that table with addresses will share primary keys along all users? Thats bad. Each user will have his own list of addresses and if there are more users, each one with multiple addresses, some of their Lists will not start at index 1. Also when additional addresses are added, their IDs will not probably be consecutive as new addresses of other users are being added.
@Siatkowkarzadzi2 жыл бұрын
There's nothing bad about that. Id is just id, it does not have to be consecutive. Unless you have some weird requirement regarding that, but it would be difficult to maintain
@iTo1ik4 жыл бұрын
This one works for me as well: @GenericGenerator( name = "user_addr_gen", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = { @Parameter(name = "sequence_name", value = "user_addr_seq"), @Parameter(name = "initial_value", value = "1"), @Parameter(name = "increment_size", value = "1") } ) @CollectionId(columns = {@Column(name = "user_details_id")}, generator = "user_addr_gen", type = @Type(type = "long"))
@vishwanathtontanal6617 Жыл бұрын
Why can't we just add "Id" column in Address class?
@XxxXxx-zs1xr9 жыл бұрын
Not yet supported. Type this: @GenericGenerator(name="sequence-gen", strategy="sequence") @CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "sequence-gen", type = @Type(type="long"))
@chenxiaotian26569 жыл бұрын
+Xxx Xxx why my first primary key is 2 not 1
@chenxiaotian26569 жыл бұрын
+Xxx Xxx problem solved.I already used generatedvalue early in the same class to generate primary key in another table. Can i still use twice generatedvalue and also start at 1?
@naveenvigneshwar5190 Жыл бұрын
thanks a lot
@ksattu16 жыл бұрын
great
@shilpag532911 жыл бұрын
I got it sir thanks
@atul300410 жыл бұрын
good
@MohammadRashid-eq2ye8 жыл бұрын
In genericGenerator, I use strategy =increment. I can solve the problem. @GenericGenerator(name = "hilo-gen", strategy = "increment")