0

我正在尝试测试一个使用休眠和注释的简单应用程序。我有以下具有以下外键约束的数据库表。

create table user_info 
(
mobile_number varchar(16) primary key, 
gender char(1),
language varchar(4))


create table user_order_info
(order_id integer(16) not null primary key auto_increment,   
mobile_number varchar(16) not null,
location_name varchar(32) not null, 
foreign key(mobile_number) references user_info(mobile_number),

create table user_queue_order_info
(queue_id integer(16) not null primary key auto_increment,
order_id integer(16),   
mobile_number varchar(16) not null,  
order_value integer(8) not null,   
foreign key(mobile_number) references user_info(mobile_number),
foreign key(order_id) references user_order_info(order_id) on delete cascade)

而上表各自的实体表如下:

@Entity
@Table(name = "user_info")
public class UserInfo implements Serializable {         
private String mobileNumber;    
private char gender;
private String language;
    //getters and setters and constructors
}


@Entity
@Table(name = "user_order_info")

public UserOrderInfo implements Serializable {  
private long orderId;
private String locationName;        
private UserInfo userInfo;
    //getters and setters and constructors
}


@Entity
@Table(name = "user_queue_order_info")

public class UserQueueOrderInfo implements Serializable {   

private long queueId;   
private int orderValue; 
private UserInfo userInfo;
private UserOrderInfo userOrderinfo;
    //getters and setters , constructors

问题是当我尝试使用 UserOrderInfoDao 通过以下方法访问数据库时,异常来了。

public UserOrderInfo UserOrderInfoByMobileNumber(String mobileNumber ) {
List<UserOrderInfo> userOrderInfoList =    sessionFactory.getCurrentSession().createQuery("from UserOrderInfo where userInfo.mobileNumber='"+mobileNumber+"'").list();
    if(!userOrderInfoList.isEmpty()){
        return userOrderInfoList.get(0);
    }
    return null;
}

当我尝试通过跟踪异常进行调试时,出现的异常是:

异常无法解析路径 [userInfo.mobileNumber],意外令牌 [userInfo] [来自 com.appls.entity.UserOrderInfo where userInfo.mobileNumber='7770909090']

为什么会出现这个问题?我正在等待您的宝贵建议。

我已经通过以下方式在 getter 和 setter 中完成了映射:

@Id 
@Column(name = "mobile_number")
public String getMobileNumber() {
    return mobileNumber;
}

其他领域也一样。

User_info 的实体是

    private String mobileNumber;        
private char gender;
private String language;

@Id 
@Column(name = "mobile_number")
public String getMobileNumber() {
    return mobileNumber;
}
public void setMobileNumber(String mobileNumber) {
    this.mobileNumber = mobileNumber;
}

@Column(name = "gender")
public char getGender() {
    return gender;
}
public void setGender(char gender) {
    this.gender = gender;
}

@Column(name = "language")
public String getLanguage() {
    return language;
}
public void setLanguage(String language) {
    this.language =language;
}

public UserInfo() {
    super();        
}


public UserInfo(String mobileNumber){
    super();
    this.mobileNumber = mobileNumber;
}

public UserInfo(String mobileNumber, char gender, String language) {
    super();
    this.mobileNumber =mobileNumber;
    this.gender = gender;
    this.language = language;

}   

@Override
public String toString() {
return "{\"mobileNumber\": \"" + this.mobileNumber
            + "\", \"gender\": \"" + this.gender
            + "\", \"language\": \"" + this.language }";
}
4

2 回答 2

2

要么改变, private String mobileNumber;包括 getter/setter

作为private String mobile_number;

或者注释它

@Column(name="mobile_number")
private String mobileNumber;  

private String locationName;和相同private String orderValue;

查询应该像 createQuery("from UserOrderInfo userOrderInfo where userOrderInfo.userInfo.mobileNumber='"+mobileNumber+"'").list()

于 2013-01-03T11:32:02.183 回答
0

您应该通过注释将您的 mobileNumber 字段更改为 DB 或 pojo 中的 mobile_number。

于 2013-01-03T11:59:38.127 回答