1

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:

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;
}
}

In this class, you see, I have interests field is array of string which showed as many of checkbox.

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

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.

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

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

Any idea about that?

Thanks!

2 Answers 2

2

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

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;

And interests could be like this

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

    @ManyToOne
    @JoinColumn(nullable = false)
    private Sample sample;
Sign up to request clarification or add additional context in comments.

5 Comments

As you said, I persist the result of calling toString on the interests array, why can Hibernate read it clearly when I read it back, although it a unreadable string? And if I change getInterests to return a formatted string, can Hibernate read it back? Need I change setInterests and split, too?
Now I changed public String getInterests() {//code here}, and I receive a error message, you know, because properties is array, the return of get method need to be a array of string.
"why can Hibernate read it clearly when I read it back, although it a unreadable string?" that doesn;t make sense. Hibernate will read it back as one string, thats why i suggest the second option. But I recommned you get a full undestanding of toString and general java OOP before starting hibernate.
I just want to save it as normal string, which I can split it by use space token to get array of strings. So, thanks for your reply!
Does it have any way which just use String class and don't need to use other entity?
0

I have looked you code and i think in "interests" field you want to store any values from array and same while retrieving from the DB. You need to use enum kind of functionality but dont know whether your DB is supporting or not ? I have the same functionality in one of my project and we have used DB enum in database, it is PostgreSQL. If your db is not supporting the enum then you have to use OneToMany and have to create separate table for Interest for mapping.

1 Comment

Thanks! I use some database system which is up to user choices. I need to think about right way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.