Adr*_*Cox 10 java hibernate jpa-2.0
我正在使用Hibernate 3.5.6作为我的JPA 2.0实现.我正在尝试@ElementCollection在我的实体内部构建(省略了许多字段):
@Entity
public class Buyer implements Serializable {
...
@ElementCollection
private List<ContactDetails> contacts;
...
}
Run Code Online (Sandbox Code Playgroud)
当集合包含基本类型时,我很容易完成这项工作,但我ContactDetails是一个@Embeddable类:
@Embeddable
public class ContactDetails implements Serializable {
...
@Column(nullable = false)
private String streetOne;
....
}
Run Code Online (Sandbox Code Playgroud)
当我运行让Hibernate生成DDL时,我得到这样的错误:
INFO - Environment - Hibernate 3.5.6-Final
....
INFO - Version - Hibernate EntityManager 3.5.6-Final
....
INFO - SettingsFactory - RDBMS: PostgreSQL, version: 8.4.2
INFO - SettingsFactory - JDBC driver: PostgreSQL Native Driver, version: PostgreSQL 9.0 JDBC4 (build 801)
INFO - Dialect - Using dialect: org.hibernate.dialect.PostgreSQLDialect
....
ERROR - SchemaUpdate - Unsuccessful: create table Buyer_contacts (Buyer_id int8 not null, contacts_collection&&element_county varchar(255), contacts_collection&&element_email varchar(255), contacts_collection&&element_fax varchar(255), contacts_collection&&element_mainphone varchar(255) not null, contacts_collection&&element_mobile varchar(255), contacts_collection&&element_name varchar(255) not null, contacts_collection&&element_postcode varchar(255) not null, contacts_collection&&element_streetone varchar(255) not null, contacts_collection&&element_streettwo varchar(255), contacts_collection&&element_town varchar(255) not null)
ERROR - SchemaUpdate - ERROR: syntax error at or near "&&" Position: 73
Run Code Online (Sandbox Code Playgroud)
有没有办法说服Hibernate在集合类的表中生成有效的列名?理想情况下,通过指定每个单独的列名称,这种方式不违反" 不要重复自己"原则.
axt*_*avt 12
这是Hibernate中由于DefaultComponentSafeNamingStrategy与@ElementCollection实现细节不兼容而导致的错误.
.collection&&element.是一个内部占位符,应在将属性名称用作列名之前删除.其他命名策略通过仅使用最后一个属性名称的一部分来有效地删除它.,而DefaultComponentSafeNamingStrategy用.s 替换_s但不删除占位符.
如果您确实需要DefaultComponentSafeNamingStrategy,这是一个解决方法:
public class FixedDefaultComponentSafeNamingStrategy extends DefaultComponentSafeNamingStrategy {
@Override
public String propertyToColumnName(String propertyName) {
return super.propertyToColumnName(
propertyName.replace(".collection&&element.", "."));
}
}
Run Code Online (Sandbox Code Playgroud)
报道:HHH-6005.
| 归档时间: |
|
| 查看次数: |
2977 次 |
| 最近记录: |