-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 24 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Incorrect string value: '\xAC\xED\x00\x05ur...' for column
PostPosted: Wed Dec 06, 2017 6:28 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Code:
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})   
@ElementCollection   


This mapping is not valid.

1. @ManyToMany is for entities
2. @ElementCollection is for mapping basic and embeddable types.

In your case, you should use @ElementCollection if you need a List<String> or use @ManyToMany if you need a List<StringWrapper> where StringWrapper is a Child entity to wraps the String value you need.


Top
 Profile  
 
 Post subject: Re: Incorrect string value: '\xAC\xED\x00\x05ur...' for column
PostPosted: Mon Dec 25, 2017 10:50 am 
Beginner
Beginner

Joined: Tue Aug 08, 2017 12:14 am
Posts: 44
vlad wrote:
Quote:
Do I need to use join table since mine is a many to many association ?


It's not a many-to-many, but a one-to-many association. ElementCollection does not scale very well and requires an intermediary table.

You could use a separate entity that only has:

- an identifier
- the String value
- the parent_id FK

This way, you can map the parent_id as @ManyToOne:

Code:
@ManyToOne
private Parent parent:


and turn the List<String> into a List<Child> mapped as @OneTomany(mappedBy = "parent")


Hi vlad,

Sorry I was busy with my pc repair cos of harddisk crash.
And now I am back but still stuck at this piece of work.

Could you kindly explain what is String value ?

Do you mean String subjName which I have used in my controller which I tried to get the Parameter values ?

And the turn the List<String> into a List<Child> to List<subjName> ?

So, do I now do the mapping at this new entity or at the tutor entity and subject entity ?

Hope you could clarify.

Thanks again.


Top
 Profile  
 
 Post subject: Re: Incorrect string value: '\xAC\xED\x00\x05ur...' for column
PostPosted: Mon Dec 25, 2017 1:21 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Just turn the List<String> into a List<Child> and it will work.

Just read the bidirectional @OneToMany section and do the mapping like that.

So,

1. The Parent entity will have a List<Child> using mappedBy.
2. The Child will map the Parent using @ManyToOne.

It's just a typical one-to-many table relationship based on a FK.


Top
 Profile  
 
 Post subject: Re: Incorrect string value: '\xAC\xED\x00\x05ur...' for column
PostPosted: Thu Dec 28, 2017 6:36 am 
Beginner
Beginner

Joined: Tue Aug 08, 2017 12:14 am
Posts: 44
Hi vlad,

I did as you said by following the example in your book.

But, I am getting an error message from IDE :

Quote:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to model.Subject
at model.Tutor.addSubject(Tutor.java:190)
at controller.tutorController.doPost(tutorController.java:79)


Code:
This is from my Tutor class :

public List<Subject>getSubjects(){
      return subjNames;
   }
   
   public void addSubject(List<Subject> list){
      subjNames.add((Subject) list);
      ((Subject) list).setTutors(this);   
   }
   
   public void removeSubject(Subject subject){
      subjNames.remove(subject);
      subject.setTutors(null);
   }


Code:
Tutor m = new Tutor();
......
String[]subject = request.getParameterValues("subject");   
         List<String>subjects = new ArrayList<String>(Arrays.asList(subject));
         @SuppressWarnings("rawtypes")
         List<Subject> objectList = (List)subjects;
         m.addSubject(objectList);   


Is there anything that I have not been following correctly ?


Top
 Profile  
 
 Post subject: Re: Incorrect string value: '\xAC\xED\x00\x05ur...' for column
PostPosted: Thu Dec 28, 2017 7:05 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Code:
subjNames.add((Subject) list);


This doesn't look right and that's why you get the Exception.

If you had a List<Subject>, you'd call:

Code:
subjNames.addAll(list)


If you had a Subject instance, you'd call:

Code:
subjNames.add(subject)


Now, back to your code:

Code:
List<String>subjects = new ArrayList<String>(Arrays.asList(subject));
@SuppressWarnings("rawtypes")
List<Subject> objectList = (List)subjects;
m.addSubject(objectList); 


This part is useless:

Code:
@SuppressWarnings("rawtypes")
List<Subject> objectList = (List)subjects;


And this is how you add a list of Subjects:

Code:
List<String>subjects = new ArrayList<String>(Arrays.asList(subject));
for(Subject subject : subjects) {
    m.addSubject(subject);
}


I think you should read Effective Java from Joshua Bloch first. Then learn Hibernate, Spring, REST, etc.


Top
 Profile  
 
 Post subject: Re: Incorrect string value: '\xAC\xED\x00\x05ur...' for column
PostPosted: Thu Dec 28, 2017 11:34 am 
Beginner
Beginner

Joined: Tue Aug 08, 2017 12:14 am
Posts: 44
vlad wrote:
Code:
subjNames.add((Subject) list);




And this is how you add a list of Subjects:

Code:
List<String>subjects = new ArrayList<String>(Arrays.asList(subject));
for(Subject subject : subjects) {
    m.addSubject(subject);
}


I think you should read Effective Java from Joshua Bloch first. Then learn Hibernate, Spring, REST, etc.


Hi vlad,

From what I have studied before, I can only do the above like what you said when I can convert List<String>subjects into a List<Subject> subjlist.

So, this is the part that I am stuck cos I googled everywhere just can't find anything that allow me to do the conversion.


Top
 Profile  
 
 Post subject: Re: Incorrect string value: '\xAC\xED\x00\x05ur...' for column
PostPosted: Thu Dec 28, 2017 12:09 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Code:
List<String> subjectStringList = new ArrayList<String>(Arrays.asList(subject));

List<Subject> subjects = getSubjects(subjectStringList);

for(String subjectValue : subjects) {
    m.addSubject(new Subject(subjectValue));
}


Just implement a Subject constructor that populates the fields based on the encoded String value.


Top
 Profile  
 
 Post subject: Re: Incorrect string value: '\xAC\xED\x00\x05ur...' for column
PostPosted: Fri Dec 29, 2017 9:40 am 
Beginner
Beginner

Joined: Tue Aug 08, 2017 12:14 am
Posts: 44
vlad wrote:
Code:
subjNames.add((Subject) list);


This doesn't look right and that's why you get the Exception.

If you had a List<Subject>, you'd call:

Code:
subjNames.addAll(list)


If you had a Subject instance, you'd call:

Code:
subjNames.add(subject)



Hi vliad,

I would like to clarify the above.

So, now I am using a constructor in my Subject class with a field variable String subject.

Therefore, in my Tutor class, I can't write this below as per your example right ?

public List<Subject>getSubjects(){
return subjNames;
}

public void addSubject(Subject subject){
subjNames.addAll(subjNames);
//subjNames.add((Subject) list);
((Subject) subjNames).setTutor(this);
//((Subject) list).setTutors(this);
}

public void removeSubject(Subject subject){
subjNames.remove(subject);
subject.setTutor(null);
}

cos with the above code, I am getting a java.lang.NullPointerException
at model.Tutor.addSubject(Tutor.java:184)
at controller.tutorController.doPost(tutorController.java:75)

In this case how should I write the add and remove portion ?


Top
 Profile  
 
 Post subject: Re: Incorrect string value: '\xAC\xED\x00\x05ur...' for column
PostPosted: Fri Dec 29, 2017 2:12 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Code:
subjNames.addAll(subjNames);


What's the meaning for this line of code?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 24 posts ]  Go to page Previous  1, 2

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.