jpa mysql size,如何在使用JPA进行映射时增加mysql中String的长度

I'm having some trouble in JPA. I would be really thankful if someone could provide a solution. WIth JPA (I'm using MySQL DB), lets say, I have a class mapped as follows:

@Entity

class Employee{

int id;

String employeeName;

//getters and setters...

}

when mapping to the table, I see that String is mapped with varchar(255) in Mysql. However, lets say if I have an employee whose name is more than 255 chars, it shows data truncation error.

I know we could solve this by adding "length" attribute to the Employee column as:

@column(length=1000)

String employeeName;

Is this the only possible way? I thought, if we just map to String in java, the database would dynamically assign length.

解决方案when mapping to the table, I see that String is mapped with varchar(255) in Mysql

This is because the default length of VARCHAR columns in DDL statements created by most JPA providers (including Hibernate and EclipseLink) is 255. Specifying the length attribute to the @Column annotation helps override the value, so that the new value is picked up by the schema generator of the JPA provider.

I thought, if we just map to String in java, the database would dynamically assign length.

This is an incorrect assumption. The JPA provider will create tables only once, and will not dynamically change the length of the underlying table during the lifetime of the application, and only if you configure the provider to create/update the table definitions in the first place. Moreover, the default mapping of String is the SQL VARCHAR type.

You seem to have configured the JPA provider to create tables as needed (after possibly dropping them), during the initialization process. If you're using Hibernate, this is done using the hibernate.hbm2ddl.auto property specified in persistence.xml with a value of update, create or create-drop. With EclipseLink, you would be specifying the property eclipselink.ddl-generation with a value of create-tables or drop-and-create-tables.

Both of the above properties are not recommended for use in a production environment. The ideal approach is to have DDL scripts to create the tables. Since, you are using VARCHAR, you ought to specify a suitable length in the column definition, to fit the maximum length of the user input. Additionally, since you are using VARCHAR over CHAR, the database engine will ensure that the storage space allocated will depend on the size of the records being stored.

If you do not need a String to the default VARCHAR mapping and instead use another valid mapping, then you must use columnDefinition attribute of the @Column annotation. An example usage for mapping Calendar to the TIMESTAMPTZ SQL datatype is shown in the JPA WikiBook; you'll need to modify this to suit your need.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值