2013年7月30日星期二

BAE environment hibernate annotations Configuration

Configuring first need to add annotations hibernate-jpa-2.0-api-1.0.1.Final.jar and ejb3-persistence.jar these two packages

ejb3-persistence.jar provides javax.persistence. *

hibernate-jpa-2.0-api-1.0.1.Final.jar provides a Hibernate right annotation support

hibernate.cfg.xml configuration then add the following code Add POJO persistence class mapping

<mapping class="bean.XXX" />

can then be persistent bean.XXX class of

But after

may find a problem, especially in the windows originally developed. The problem is that the database table name capitalization issue, under the windows mysq l default table names are case-insensitive, but in linux mysql default is case sensitive, although you can through the mysql configuration file my.ini to be modified, in [mysqld] added after

lower_case_table_names=1

current environment is BAE, obviously not modify my.ini

Because Hibernate hql then parse the table name when converted into pure lowercase, but when the automatically generated table if you do not modify the default mapping table name, then the table will be generated based on the class name, which leads in practice to make the database when the operation results in an error:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'llXXxMuiXhSwIXFSiXxk.users' doesn't exist

such an action would need to get rid of the default table name, such lowercase generated tables and table name on the actual operation agreement.

Here again, however there may be a mistake:

import org.hibernate.annotations.Table; 
/**
* Users entity.
@author MyEclipse Persistence Tools
*/
@Entity
@Table(appliesTo
="Users")

many people may have been so configured, but the @ org.hibernate.annotations.Table is a complement, not a replacement to @ javax.persistence. Table

@ org.hibernate.annotations.Table it's just a supplement not replace @ javax.persistence.Table.

appliesTo if you want to modify the values ??to modify the mapping table name, an error occurs, the error does not result in BAE can not be released, but will appear when accessing the site 404, you will find by looking at the log WARNING, the error is:

org.hibernate.AnnotationException: @org.hibernate.annotations.Table references an unknown table: users

actually want to change the default table name, you must use the @ javax.persistence.Table, the code is as follows:

import javax.persistence.Table; 

/**
* Users entity.
@author MyEclipse Persistence Tools
*/
@Entity
@Table(name
="users")

thus modify the default mapping out the table name, the database can also be used normally

1 条评论:

  1. f you don't understand my answer, don't ignore it, ask a question.

    回复删除