I use Eclipse --> JPA Entities from Tables get generate entities from mysql database. All the tables' primary keys -- ids are int(11) AI PK. So instead of int or long, i am getting String for all the keys. What did i do wrong?
Thanks!
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="LOCATION_ID")
private String locationId;
Eclipse is not correctly parsing your primary key's data type (int(11)). As a result, Eclipse is unable to resolve the appropriate data type to Java type mapping. Because that mapping is missing, Eclipse defaults the datatype for primary key attributes to String. You could remove the display width attribute from your datatype (i.e. the (11)) and Eclipse should be able to more appropriately map the data type.
Just found out when generating the entity, click on the id and eclipselink will allow you to select the mapping type. Choose int instead of String will do the trick.
Related
I need to create a auto-incremented key (not a primary key) to use it as a file-name in spring-data
here what i tried:
#NotNull
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "BODY_FILE")
private long bodyFile;
in liquibase:
- column:
name: BODY_FILE
type: BIGINT
autoIncrement: true
but the field bodyfile is always 0.
#Generated value will ONLY work with a primary key.
It won't work with fields that are not a primary key.
It won't even work with the composite primary key.
But why not generated type can be applied for the non-primary field?
A possible answer is most of the older version of DB either does not support the AUTO_INCREMENT (Generated value) for the non-primary key field or If they support in a newer version that too has constraints- like for MySQL There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value.
So from where this zero comes from in your DB?
It is because of your datatype 'Long' its default value is getting stored in your DB.
For more details on #Generated value official documentation
I was generating Entity class using JPA Tools in eclipse Mars. It generates data member of Integer datatype which is smallint type in table.I need to generate short type of datatype for column which is smallint in table.
I think something like this might work :-
#Column(columnDefinition = "SMALLINT")
#Type(type = "org.hibernate.type.ShortType")
private short variableName;
After entering JPA entities from table wizard after selecting table->table Association->Customize Defaults->customize indicidual Entities .. here you can customize your table
But usually it ll automatically convert smallint type to short.
I am trying to create a Spring Roo entity for a legacy database table. The table does not have a primary key defined.
However, Roo will not let me define an entity without an identifierField:
#RooEntity(identifierColumn = "", identifierField = "", table = "XYZ", versionField = "")
This causes the Roo integration tests to fail with:
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'id' in 'field list'
Because it creates an #Id and #Column(name = "id") field in Roo_Entity.aj.
I have tried reverse engineering this table with Roo's DBRE function, but all it does is create an identifier class containing all of the fields of the entity. When this happens I cannot add finders for the individual table columns.
As far as I know Roo requires a primary key and strongly suggests a version identifier.
It's possible to use either a simple or a combined primary key. Also looking at DBRE documentation, there is no mention about tables without primary key.
What about fixing the DB? Which DBMS is it? Is it feasible to add a numeric identifier, possibly auto-generated?
How can I declare within an Entity Bean a Long Text attribute?
For example: String description
I would like it to be more than 255 varchar, and maybe mapped on MySQL as TEXT.
Thanks
Solved:
JPA: how do I persist a String into a database field, type MYSQL Text
Using #Lob annotation
Just define the corresponding column in database as TEXT, and JPA should happily map the column to your attribute.
I have the following definition for an id field in an entity that is mapped to a table in HSQLDB.
...
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name = "ID")
private Integer id;
...
But this does not seem to generate the an unique id; instead an attempt is made to insert null into the column which results in failure. If, I manually create a sequence and generation strategy to use that sequence then the data is persisted as expected.
Doesn't a generation strategy of auto imply that the provider (hibernate in this case) will automatically choose the correct approach and do all the heavy lifting as needed (create sequence, use a native approach or whatever works for that particular platform)? Is my understanding incorrect?
Doesn't a generation strategy of auto imply that the provider (hibernate in this case) will automatically choose the correct approach and do all the heavy lifting as needed (create sequence, use a native approach or whatever works for that particular platform)? Is my understanding incorrect?
It does in theory (it defaults to IDENTITY with HSQLDB) and it works for me. This begs the following questions:
What dialect are you using (just in case)?
How did you create the table?
Can you show the DDL (activate the logging of org.hibernate.tool.hbm2ddl if required)?
How do you insert (through Hibernate's API, right?)?
Here is a sample DDL for an entity Foo when using HSQLDB:
create table Foo (
id bigint generated by default as identity (start with 1),
bar varchar(100),
primary key (id)
)
I created the table using the HSQL DB manager. Just normal create table address... I had not set the id column as identity in my case - just set it as primary key.
Then you have your answer, use an IDENTITY column.
While Hibernate does choose the right strategy and does generate the appropriate INSERT statements (passing null into the id which is expected to be persisted into an IDENTITY column), it won't create or alter your physical model if you don't use the DDL generation and export capabilities.
I had the same issue when using a JpaSchemaGenerator utility class that I wrote.
When generating the schema for a org.hibernate.dialect.HSQLDialect (where I use a SEQUENCE to generate my unique IDs), I use the following Hibernate property:
hibernate.id.new_generator_mappings=true
This results in the following CREATE statement:
CREATE TABLE BATCH (
BAT_ID NUMBER(19,0) NOT NULL,
BAT_EXPIRY_DATE TIMESTAMP,
BAT_NUMBER VARCHAR2(255 CHAR),
BAT_MAT_ID NUMBER(19,0),
PRIMARY KEY (BAT_ID)
);
But when I use this same property in my utility class to generate a schema using the org.hibernate.dialect.HSQLDialect, I get the following CREATE statement:
CREATE TABLE BATCH (
BAT_ID BIGINT NOT NULL,
BAT_EXPIRY_DATE TIMESTAMP,
BAT_NUMBER VARCHAR(255),
BAT_MAT_ID BIGINT,
PRIMARY KEY (BAT_ID)
);
This would mean that if I created a Batch without an ID, it would not generate it for me and the NOT NULL constraint would cause an exception.
If I change the Hibernate property to the following:
hibernate.id.new_generator_mappings=false
Then it would generate the following CREATE statement:
CREATE TABLE BATCH (
BAT_ID BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 1),
BAT_EXPIRY_DATE TIMESTAMP,
BAT_NUMBER VARCHAR(255),
BAT_MAT_ID BIGINT,
PRIMARY KEY (BAT_ID)
);
Which works perfectly when creating JPA entities with Hibernate.