Hibernate Tutorial 11 - Configuring Collections and Adding Keys

preview_player
Показать описание
More configuration options for collections, including configuring primary keys.
Рекомендации по теме
Комментарии
Автор

sir, your explanation is very clean and can understand carefully what you can say.thank you

SamithDilharaF
Автор

Thank you so much Koushik. These are very good tutorials for beginners.

bhaskaryerraguntla
Автор

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(-).

pareek.himanshu
Автор

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?

mustafazayed
Автор

Thank you very much. for those who watching this since hilo got deprecated, use sequence-gen instead of hilo.

TheGuroguro
Автор

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

mamlazz
Автор

Seems, hi-lo is not supported, and "sequence" worked for me:

@ElementCollection
@GenericGenerator(name = "sequence", strategy = "sequence")
@GeneratedValue(generator = "sequence",

@CollectionId(columns = { @Column(name="ADDRESS_ID") }, generator = "sequence", type = @Type(type = "long") )
//private Set<Address> addrSet= new HashSet<Address>();
private Collection<Address> listOfAddresses = new ArrayList<Address>();

pradeepreddy
Автор

@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).

Java.Brains
Автор

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?

itsagame
Автор

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

Tomyxbox
Автор

In genericGenerator, I use strategy =increment. I can solve the problem. @GenericGenerator(name = "hilo-gen", strategy = "increment")

tonythuanho
Автор

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",
And again I can’t be grateful enough to koushik.

bkg_random
Автор

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.

btharunreddy
Автор

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.

khoanguyen-rusp
Автор

Did Hibernate generate an additional table called 'hibernate_unique_key' for you guys as well?

amsfuy
Автор

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?

kashyapsumeet
Автор

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.

skazis
Автор

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.

techdesigndedics
Автор

Hi sir, i am getting the following error "Exception in thread "main" No identifier specified for entity:"
Thanks in advance.

karanac
Автор

Why can't we just add "Id" column in Address class?

vishwanathtontanal