Hibernate Tips is a series of posts in which I describe a quick and easy solution for common Hibernate questions. If you have a question for a future Hibernate Tip, please leave a comment below. Question: You explained that the GenerationType.SEQUENCE is the most efficient primary key generation strategy and that MySQL doesn’t support it. Aug 01, 2016 One of my readers asked me to help him map a Composite Primary Key using JPA and Hibernate. Because this is a recurrent question, I decided to write a blog post in which I describe this mapping is more detail. Domain Model. A relational database composite key contains two or more columns which together for the primary key of a given table.
I have a table which has no primary key. How to create the hibernate mapping xml without using the generator id
Ranch Hand
posted 14 years ago
Every relational table has a 'natural' primary key. However, it may be unrecognized and undefined. The best solution to your problem then is to determine what the primary key is and define it as such in your RDBMS. Having said that, it is better to use a surrogate key for the primary key to protect against business semantics changes in the natural key. So why not use a generated id?
Bartender
posted 14 years ago
Edwin Keeton's suggestion is good. Using a surrogate makes perfect sense; fix the invalid data model so it truly represents relational data. However, if you cannot change the data model, you can map the entire table as one big composite key. A horrible solution, but a work around which gives you time to fire the DBA who modelled your data this way, and hire a competent replacement.
ranger
posted 14 years ago
'Ehrenfrids'- Welcome to the JavaRanch! Please adjust your displayed name to meet the JavaRanch Naming Policy. You can change it here. Thanks! and welcome to the JavaRanch! Mark
Perfect World Programming, LLC - iOS Apps How to Ask Questions the Smart Way FAQ
Ranch Hand
posted 14 years ago
Originally posted by Ehrenfrids: I have a table which has no primary key. How to create the hibernate mapping xml without using the generator id
How are you accessing records in that table when not using Hibernate? i.e. when you use SQL directly.
Ranch Hand
posted 14 years ago
Well, when you don't have a primary key and want to access a specific row, I suppose your query will contain all fields, right? So, as weird as it may seem, you could use a composite-key with all fields in it.
Ranch Hand
posted 14 years ago
Now, that's for a key not using the generator! For generating a value, you would have to create a field in the DB and use is as your PK.
Greenhorn
posted 13 years ago
The problem might not be so simple to adress. Suppose I work with a legacy database and I have a readonly table (imported from a back-office) and I don't want to access individual rows in that table (I want just to list the records obtained form a join). So: I can't add surogate key (I can't change the database) and I don't have a natural (business) key. What about that?
Bartender
posted 13 years ago
Originally posted by Ionescu Victor: The problem might not be so simple to adress. Suppose I work with a legacy database and I have a readonly table (imported from a back-office) and I don't want to access individual rows in that table (I want just to list the records obtained form a join). So: I can't add surogate key (I can't change the database) and I don't have a natural (business) key. What about that?
See my earlier answer. That's the only work around you have available to you.
Hibernate Manytomany The Join Table Has Sequence Generated Primary Key Mean
Greenhorn
posted 13 years ago
Well, I've a legacy database. I don't care to have a IF/Primary key. is there a way? or Hibernate just takes away that option?
Greenhorn
posted 13 years ago
I tried using a big composite key. Hibernate generates a column on MySql.In development, may be I can live with that, but not in production. I'm no expert on Hibernate but this should be easy.
Bartender
posted 13 years ago
I'm no expert on Hibernate but this should be easy
An Object-Relational Mapping tool can only be expected to map relational data. Data without a primary key it is not relational, so it should be no surprise it doesn't work.
Greenhorn
posted 10 years ago
As the table has no primary key, it should not be mapped as an entity. You could use a named query to insert data instead of declaring an entity, and perform the insert from the dao.
Greenhorn
posted 8 years ago
ORM is to map relations through primary and foreign key to database. If you are not having those on tables then you can use, criteria search with HQL or SQL and populated fetched data into related objects. These objects can be used for further. Example can be found at http://www.deepakgaikwad.net/?p=448
Every JPA entity is required to have a field which maps to primary key of the database table. Such field must be annotated with @Id.
Simple vs Composite primary keys
A simple primary key consists of a single Java field which maps to a single table column. A composite primary key consists of multiple Java fields which individually map to separate columns.
Supported types for a primary key
A simple primary key field or one of the composite primary key field should be one of the following types:
Any Java primitive type
any Any primitive wrapper type
java.lang.String
java.util.Date
java.sql.Date
java.math.BigDecimal
java.math.BigInteger
In this tutorial we are going to focus on generation strategies of simple primary key.
@GeneratedValue Annotation
This annotation defines the types of primary key generation strategies. If this annotation is not used then application is responsible to populate and manage @Id field values itself.
The use of the GeneratedValue annotation is only required to be supported for simple primary keys.
GenerationType enum defines four strategies: Generation Type . TABLE, Generation Type. SEQUENCE, Generation Type. IDENTITY and Generation Type. AUTO. Let's understand them with examples.
GenerationType.SEQUENCE
With this strategy, underlying persistence provider must use a database sequence to get the next unique primary key for the entities.
We have created the following Util class to reuse the code for other examples.
Also, in the persistence.xml, we have created four persistence-unit, so that we can try four GenerationType independently. We are using Hibernate as persistence provider.
Let's create main class to try out Entity1 key generation.
Output
Above output shows one table MYENTITY1 and one sequence HIBERNATE_SEQUENCE are created.
GenerationType.TABLE
With this strategy, underlying persistence provider must use a database table to generate/keep the next unique primary key for the entities.
Output
This time no sequence is generated, instead an additional table named 'HIBERNATE_SEQUENCES' is created to maintain primary key sequence.
GenerationType.IDENTITY
This GenerationType indicates that the persistence provider must assign primary keys for the entity using a database identity column. IDENTITY column is typically used in SQL Server. This special type column is populated internally by the table itself without using a separate sequence. If underlying database doesn't support IDENTITY column or some similar variant then the persistence provider can choose an alternative appropriate strategy. In this examples we are using H2 database which doesn't support IDENTITY column.
Output
Above output shows that a sequence is used for primary keys.
GenerationType.AUTO
This GenerationType indicates that the persistence provider should automatically pick an appropriate strategy for the particular database. This is the default GenerationType, i.e. if we just use @GeneratedValue annotation then this value of GenerationType will be used.
Output
Above output shows that a sequence is used for primary keys.
When @GeneratedValue not used
If we don't use @GeneratedValue annotation at all, then we have to populate the unique primary keys ourselves. In this example, we are simply assigning it to the value returned from System.nanoTime()
Output
Above output shows that a no sequence or extra table were generated.
Example Project
Dependencies and Technologies Used:
h2 1.4.193: H2 Database Engine.
hibernate-core 5.2.8.Final: The core O/RM functionality as provided by Hibernate. Implements javax.persistence:javax.persistence-api version 2.1