Hibernate Tutorial 11 - Configuring Collections and Adding Keys

  Рет қаралды 241,478

Java Brains

Java Brains

Күн бұрын

Пікірлер
@SamithDilharaF
@SamithDilharaF 12 жыл бұрын
sir,your explanation is very clean and can understand carefully what you can say.thank you
@TheGuroguro12
@TheGuroguro12 7 жыл бұрын
Thank you very much. for those who watching this since hilo got deprecated, use sequence-gen instead of hilo.
@bhaskaryerraguntla2368
@bhaskaryerraguntla2368 9 жыл бұрын
Thank you so much Koushik. These are very good tutorials for beginners.
@mustafazayed8308
@mustafazayed8308 7 ай бұрын
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.himanshu
@pareek.himanshu 4 жыл бұрын
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(-).
@mamlazz2
@mamlazz2 7 жыл бұрын
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.Brains
@Java.Brains 13 жыл бұрын
@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).
@Tomy97xbox
@Tomy97xbox 7 жыл бұрын
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
@pradeepreddy9405
@pradeepreddy9405 9 жыл бұрын
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();
@gillianbc
@gillianbc 9 жыл бұрын
+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'"
@gillianbc
@gillianbc 9 жыл бұрын
+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
@chenxiaotian2656
@chenxiaotian2656 9 жыл бұрын
+Pradeep Reddy why my first primary key is 2 ,not 1
@chenxiaotian2656
@chenxiaotian2656 9 жыл бұрын
+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?
@chenxiaotian2656
@chenxiaotian2656 9 жыл бұрын
+Pradeep Reddy problem solved.I use type.table instead of sequence
@bkg_random
@bkg_random 5 жыл бұрын
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.
@tonythuanho8077
@tonythuanho8077 11 жыл бұрын
In genericGenerator, I use strategy =increment. I can solve the problem. @GenericGenerator(name = "hilo-gen", strategy = "increment")
@itsagame91
@itsagame91 9 жыл бұрын
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-ns2oc
@ShivaKumar-ns2oc 8 жыл бұрын
+mayank chawla I tried the same, it hasn't worked. sequence-gen is working good. Kaushik, could you put some light into this?
@saurabhbhattacharya1217
@saurabhbhattacharya1217 5 жыл бұрын
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.
@btharunreddy
@btharunreddy 9 жыл бұрын
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.
@borisjurosevic177
@borisjurosevic177 11 жыл бұрын
great tutorial sir
@amsfuy
@amsfuy 11 жыл бұрын
Did Hibernate generate an additional table called 'hibernate_unique_key' for you guys as well?
@kashyapsumeet
@kashyapsumeet 11 жыл бұрын
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?
@techdesigndedics
@techdesigndedics 9 жыл бұрын
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.
@kapoeiradobrasil
@kapoeiradobrasil 9 жыл бұрын
+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-ru5sp
@khoanguyen-ru5sp 10 жыл бұрын
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.
@karanac3582
@karanac3582 5 жыл бұрын
Hi sir , i am getting the following error "Exception in thread "main" org.hibernate.AnnotationException: No identifier specified for entity:" Thanks in advance.
@hashiroxx
@hashiroxx 11 жыл бұрын
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..
@pushpendradwivedi7028
@pushpendradwivedi7028 10 жыл бұрын
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.
@skazis87
@skazis87 12 жыл бұрын
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.
@prativkumarhumnabad7115
@prativkumarhumnabad7115 11 жыл бұрын
thanks sir for providing woderfull videos.
@shilpag5329
@shilpag5329 11 жыл бұрын
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-uc4kw
@Vikas-uc4kw 8 жыл бұрын
@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")
@asashish905
@asashish905 5 жыл бұрын
Use strategy as 'increment' in case of MySQL.
@Iwitrag
@Iwitrag 5 жыл бұрын
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.
@Siatkowkarzadzi
@Siatkowkarzadzi 2 жыл бұрын
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
@iTo1ik
@iTo1ik 4 жыл бұрын
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
@vishwanathtontanal6617 Жыл бұрын
Why can't we just add "Id" column in Address class?
@XxxXxx-zs1xr
@XxxXxx-zs1xr 9 жыл бұрын
Not yet supported. Type this: @GenericGenerator(name="sequence-gen", strategy="sequence") @CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "sequence-gen", type = @Type(type="long"))
@chenxiaotian2656
@chenxiaotian2656 9 жыл бұрын
+Xxx Xxx why my first primary key is 2 not 1
@chenxiaotian2656
@chenxiaotian2656 9 жыл бұрын
+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
@naveenvigneshwar5190 Жыл бұрын
thanks a lot
@ksattu1
@ksattu1 6 жыл бұрын
great
@shilpag5329
@shilpag5329 11 жыл бұрын
I got it sir thanks
@atul3004
@atul3004 10 жыл бұрын
good
@MohammadRashid-eq2ye
@MohammadRashid-eq2ye 8 жыл бұрын
In genericGenerator, I use strategy =increment. I can solve the problem. @GenericGenerator(name = "hilo-gen", strategy = "increment")
Hibernate Tutorial 17 - Implementing Inheritance
14:59
Java Brains
Рет қаралды 191 М.
УНО Реверс в Амонг Ас : игра на выбывание
0:19
Фани Хани
Рет қаралды 1,3 МЛН
Как Ходили родители в ШКОЛУ!
0:49
Family Box
Рет қаралды 2,3 МЛН
Building Fyrox: A Rust Game Engine (with Dmitry Stepanov)
1:43:47
Developer Voices
Рет қаралды 1 М.
Hibernate Tutorial 16 - CascadeTypes and Other Things
15:22
Java Brains
Рет қаралды 221 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 907 М.
Hibernate Tutorial 15 - mappedBy and Many To Many Mapping
13:22
Java Brains
Рет қаралды 251 М.
Solving one of PostgreSQL's biggest weaknesses.
17:12
Dreams of Code
Рет қаралды 223 М.
Learn Database Normalization - 1NF, 2NF, 3NF, 4NF, 5NF
28:34
Decomplexify
Рет қаралды 2,2 МЛН
Hibernate & JPA Tutorial - Crash Course
24:27
Marco Codes
Рет қаралды 119 М.
Microservices explained - the What, Why and How?
18:30
TechWorld with Nana
Рет қаралды 905 М.
УНО Реверс в Амонг Ас : игра на выбывание
0:19
Фани Хани
Рет қаралды 1,3 МЛН