在Hibernate中向数据库添加字符串数组 [英] Add array of string to database in Hibernate

查看:189
本文介绍了在Hibernate中向数据库添加字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate框架的初学者,现在我在一个项目中集成了Spring和Hibernate。
我跑了就行了,但它有一些我不明白为什么。问题是:

I'm beginner in Hibernate framework, and now I integrated Spring and Hibernate in one project. I ran it OK, but it have something I can't understand why. The problem is:

我有bean类在这里描述:

I have bean class decribed here :

@Entity
@Table(name = "Sample")
public class Sample {
    private int id;
    private String firstName;
    private String lastName;
    private String sex;
    private String[] interests;

@Id
@GeneratedValue
@Column(name = "id")
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@Column(name = "firstName")
public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

@Column(name = "lastName")
public String getLastName() {
    return lastName;
}

public void setSex(String sex) {
    this.sex = sex;
}

@Column(name = "sex")
public String getSex() {
    return sex;
}

public void setInterests(String[] interests) {
    this.interests = interests;
}

@Column(name = "interests")
public String[] getInterests() {
    return interests;
}
}

在这个类中,

在DAO中,当控制器调用保存对象到数据库时,我只是使用 hibernateTemplate.saveOrUpdate (sample)类在Spring中持久化对象。

In DAO, when controller call to save object to database, I just use hibernateTemplate.saveOrUpdate(sample) class in Spring to persist object.

如果数据库使用默认字符集,它是OK,运行并没有任何错误,当我在数据库系统中选择表时,它显示一个不可读的字符串,它类似于¬ur [Ljava.lang.String;ÒVçé{G xp t Quidditcht Herbology 。我认为当我想读取并填充对象时,Hibernate给它分配的占位符。

If database use default charset, it's OK, run and don't have any error but when I select table in database system, it show me a unreadable string, it's similar to ¬í ur [Ljava.lang.String;­ÒVçé{G xp t Quidditcht Herbology . I think it's 'place holder' which Hibernate gave to split when I want to read and fill back in object.

但是问题是如果数据库系统使用utf8 charset,无法运行,并且抛出异常类似字符串值不正确:'\xAC\xED\x00\x05ur ...'用于第1行的interest列

But the problem is if database system use utf8 charset, it can't run, and throw exception similar to Incorrect string value: '\xAC\xED\x00\x05ur...' for column 'interests' at row 1

我认为因为该字符串在数据库中不支持utf8字符集。

I think because that string is not supported in database with utf8 charset.

谢谢!

推荐答案

在interest数组上调用toString的结果。

Your current code is trying to persist the result of calling toString on the interests array.

您需要设置getInterests以返回格式化的字符串或映射到不同的表作为一个关系。因为你可能想要填充数据库的兴趣我会推荐第二个选项(我有annoate做字段,但你可以很容易地改变这使用getters,事实上我shooul dprobabbly annoated我的getters):

You need to either set up you getInterests to return a formatted string or map to a different table as a onetomany relationship. As you probably want to populate interests from DB I would recommend the second option (I have annoate do fields, but you can easily change this to use getters, in fact I shoul dprobabbly annoated my getters) :

 @OneToMany(mappedBy = "sample", cascade = CascadeType.ALL)    
    private List<Interests> interests;

并且兴趣可能像这样

 @Column(nullable = false, unique = false, length = 256)
    private String text;

    @ManyToOne
    @JoinColumn(nullable = false)
    private Sample sample;

这篇关于在Hibernate中向数据库添加字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆