Hibernate4.3.6映射Oracle中的char数据类型

分享到:
  来源:急速科技  发布时间:2014-09-26  浏览次数:2205

  Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister

在网上有很多帖子说Hibernate3.x配置数据库Oracle10g映射的时候 char(10)这种数据类型时Tomcat会报错,即使配置正确了也不能正常读取数据(只能读取到字符串的第一位)

例如:

http://youngxu.iteye.com/blog/842242

http://blog.163.com/yaxctgu@126/blog/static/1626703182011714113946151/

http://blog.csdn.net/henryzhang2009/article/details/6711754

可喜的是这个问题在Hibernate4.3.6中已经解决(比这个早点的Hibernate4.x未经测试)

实体类 *.hbm.xml配置如下:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping package="com.relib.model.xml">
<class name="ExchangeUser" table="t_user" catalog="Exchange">
<id name="userID" type="string" length="15" >
<column name="userID" sql-type="char(15)"/>
<generator class="assigned" />
</id>
<property name="userName" type="string" />
 
<property name="email" type="string" />
<!--char(1)-->
<property name="tradeRight" type="java.lang.Character"/>
<!--char(1)-->
<property name="userType" type="java.lang.Character"/>
 
<!--char(20)-->
<property name="accountID" type="string">
          <column name="accountID" sql-type="char(20)" />
</property>
<property name="clientID" type="string">
         <column name="clientID" sql-type="char(20)" />
</property>
<property name="memberID" type="string">
         <column name="memberID" sql-type="char(10)" />
</property>
<property name="password" type="string">
         <column name="password" sql-type="char(40)" />
</property>
</class>
</hibernate-mapping>
 
JPA的写法:
JPA中sql-type由columnDefinition 代替
 
 
@Entity
@Table(name = "t_User", catalog = "Exchange")
public class User {
private String userID;
 
/*
* 主键的由用户自己指定: 操作:1.先从数据库中读取最大的值 2.吧最大的值加1,并赋值给新的实体类 GeneratedValue(strategy
* = GenerationType.AUTO)
*/
@Id
@GeneratedValue(generator = "userGenerator")
@GenericGenerator(name = "userGenerator", strategy = "assigned")
@Column(length = 15, columnDefinition = "char")
public String getUserID() {
return userID;
}
 
public void setUserID(String value) {
userID = value;
 
}
 
@Column(length = 10, columnDefinition = "char")
public String getMemberID() {
return memberID;
}
 
public void setMemberID(String memberID) {
this.memberID = memberID;
}
 
@Column(length = 20, columnDefinition = "char")
public String getClientID() {
return clientID;
}
 
public void setClientID(String clientID) {
this.clientID = clientID;
}
 
@Column(length = 20)
public String getUserName() {
return userName;
}
 
public void setUserName(String userName) {
this.userName = userName;
}
 
@Column(length = 20, columnDefinition = "char")
public String getAccountID() {
return accountID;
}
 
public void setAccountID(String accountID) {
this.accountID = accountID;
}
 
public String getEmail() {
return email;
}
 
public void setEmail(String email) {
this.email = email;
}
 
@Column(length = 1, columnDefinition = "char")
public char getUserType() {
return userType;
}
 
public void setUserType(char userType) {
this.userType = userType;
}
 
@Column(length = 40, columnDefinition = "char")
public String getPassword() {
return password;
}
 
public void setPassword(String password) {
this.password = password;
}
 
@Column(length = 1, columnDefinition = "char")
public char getTradeRight() {
return tradeRight;
}
 
public void setTradeRight(char tradeRight) {
this.tradeRight = tradeRight;
}
 
private String memberID;
private String clientID;
 
private String userName;
private String accountID;
private String email;
private char userType = 'a';
private String password;
private char tradeRight = 'b';
 
@Override
public String toString() {
return "User [userID=" + userID + ", username=" + userName
+ ", accountID=" + accountID + ", memberID=" + memberID
+ ", email=" + email + ", userType=" + userType + ", password="
+ password + ", tradeRight=" + tradeRight + "]";
 
}
}

 

参考:http://blog.sina.com.cn/s/blog_56d8ea9001014k8m.html

 

知识文章分类

Java

阅读排行