Joined: Tue May 04, 2010 4:38 am Posts: 4
|
What is causing a org.hibernate.exception.SQLGrammarException: could not initialize a collection: ?
I am using mysql 5.1 and hibernate 3
There is a one to many relationship causing this error and the twwo classes at play are coded as follows...
package org.bar.jar.model;
import java.util.Collection; import java.util.Set;
/** * Created by IntelliJ IDEA. * User: owner * Date: 11.mai.2010 * Time: 18:22:37 * To change this template use File | Settings | File Templates. */ public class UsersEntity { private Set<UsercommentsEntity> usercommentsesByUserName; private String userName;
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
private String firstName;
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
private String lastName;
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
private String userEmail;
public String getUserEmail() { return userEmail; }
public void setUserEmail(String userEmail) { this.userEmail = userEmail; }
private String ipAddress;
public String getIpAddress() { return ipAddress; }
public void setIpAddress(String ipAddress) { this.ipAddress = ipAddress; }
private String userPass;
public String getUserPass() { return userPass; }
public void setUserPass(String userPass) { this.userPass = userPass; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false;
UsersEntity that = (UsersEntity) o;
if (firstName != null ? !firstName.equals(that.firstName) : that.firstName != null) return false; if (ipAddress != null ? !ipAddress.equals(that.ipAddress) : that.ipAddress != null) return false; if (lastName != null ? !lastName.equals(that.lastName) : that.lastName != null) return false; if (userEmail != null ? !userEmail.equals(that.userEmail) : that.userEmail != null) return false; if (userName != null ? !userName.equals(that.userName) : that.userName != null) return false; if (userPass != null ? !userPass.equals(that.userPass) : that.userPass != null) return false;
return true; }
@Override public int hashCode() { int result = userName != null ? userName.hashCode() : 0; result = 31 * result + (firstName != null ? firstName.hashCode() : 0); result = 31 * result + (lastName != null ? lastName.hashCode() : 0); result = 31 * result + (userEmail != null ? userEmail.hashCode() : 0); result = 31 * result + (ipAddress != null ? ipAddress.hashCode() : 0); result = 31 * result + (userPass != null ? userPass.hashCode() : 0); return result; }
public Set<UsercommentsEntity> getUsercommentsesByUserName() { return usercommentsesByUserName; }
public void setUsercommentsesByUserName(Set<UsercommentsEntity> usercommentsesByUserName) { this.usercommentsesByUserName = usercommentsesByUserName; }
public void addComment(UsercommentsEntity uce){ uce.setUserId(getUserName()); usercommentsesByUserName.add(uce);
}
} package org.bar.jar.model;
import java.io.Serializable;
import java.sql.Timestamp;
/** * Created by IntelliJ IDEA. * User: owner * Date: 12.mai.2010 * Time: 20:54:44 * To change this template use File | Settings | File Templates. */ public class UsercommentsEntity implements Serializable { private int commentid;
public int getCommentid() { return commentid; }
public void setCommentid(int commentid) { this.commentid = commentid; }
private String commentmade;
public String getCommentmade() { return commentmade; }
public void setCommentmade(String commentmade) { this.commentmade = commentmade; }
private String userId;
public String getUserId() { return userId; }
public void setUserId(String userId) { this.userId = userId; }
private Timestamp datemade;
public Timestamp getDatemade() { return datemade; }
public void setDatemade(Timestamp datemade) { this.datemade = datemade; }
private boolean flagged;
public boolean isFlagged() { return flagged; }
public void setFlagged(boolean flagged) { this.flagged = flagged; }
private String pageId;
public String getPageId() { return pageId; }
public void setPageId(String pageId) { this.pageId = pageId; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false;
UsercommentsEntity that = (UsercommentsEntity) o;
if (commentid != that.commentid) return false; if (flagged != that.flagged) return false; if (commentmade != null ? !commentmade.equals(that.commentmade) : that.commentmade != null) return false; if (datemade != null ? !datemade.equals(that.datemade) : that.datemade != null) return false; if (pageId != null ? !pageId.equals(that.pageId) : that.pageId != null) return false; if (userId != null ? !userId.equals(that.userId) : that.userId != null) return false;
return true; }
@Override public int hashCode() { int result = commentid; result = 31 * result + (commentmade != null ? commentmade.hashCode() : 0); result = 31 * result + (userId != null ? userId.hashCode() : 0); result = 31 * result + (datemade != null ? datemade.hashCode() : 0); result = 31 * result + (flagged ? 1 : 0); result = 31 * result + (pageId != null ? pageId.hashCode() : 0); return result; }
private UsersEntity usersByUserId;
public UsersEntity getUsersByUserId() { return usersByUserId; }
public void setUsersByUserId(UsersEntity usersByUserId) { this.usersByUserId = usersByUserId; } }
the piece of code that fires the exception is this....
public String create() { System.out.println("GGGGGGGGGGGGGGGGGGGGGGGGG");
FacesContext context = FacesContext.getCurrentInstance(); String userId = ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getUserPrincipal().getName(); String page = (String) ((HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest()).getParameterMap().get("pageCommented"); this.getEntity().setUserId(userId); this.getEntity().setPageId(page); this.getEntity().setDatemade(new Timestamp(new Date().getTime())); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); Transaction tx=session.getTransaction();
tx.begin(); UsersEntity ue = (UsersEntity) session.load(UsersEntity.class, userId); ue.addComment(getEntity()); session.save(getEntity()); session.flush();
return "commented";
getEntity returns a instantiated commentEntity as proven by a debug stateement
I think it may have something to do with the entity that ia being pulled from the database has no previous comments or even a instantiated lset could this be the problem? }
|
|