I need to sort a Result set depending on a field by asc or dsc order. That field is of type String and contains names of users. Now the names are french name. So to sort list of users based on their names generally I use the following code:
final Collator collator = Collator.getInstance(Locale.FRANCE);
Comparator<ActivityUserDTO> comparator = new Comparator<ActivityUserDTO>() {
#Override
public int compare(ActivityUserDTO dto1, ActivityUserDTO dto2) {
return collator.compare(dto1.getFullName(), dto2.getFullName());
}
};
Collections.sort(users, comparator);
In this case I have the whole list of users loaded from Database and then I am doing the soring.
Now following code is for hibernate where I have startIndex: which sets the FirstResult for Criteria, maxResult: which sets the MaxResults of Criteria and the ordering:
Criteria criteria = getSessionFactory().getCurrentSession().createCriteria(entityClass);
if (StringUtils.isNotEmpty(sortField)) {
criteria.addOrder(sortOrder ? Order.asc(sortField) : Order.desc(sortField));
}
criteria.setFirstResult(startIndex);
criteria.setMaxResults(maxResult);
Here the sortField is fullName which is in French and sortOrder can be either true or false.
Is there any way to make the ordering in custom manner so that it do the sorting/ordering which is done by the Collator? Any pointer would be very helpful to me.
I have seen some site like:
Documentation
Sorting Hibernate Set using a comparator
Hibernate annotations sort using Comparator
where they are using Comparator to sort the Set of Assoicated Objects, but how can I do it in my case?
This is my User:
#javax.persistence.Entity
#Table(name = "USER")
public class User extends Entity {
#Transient
private static final long serialVersionUID = -112950002831333869L;
private String username;
private String firstName;
private String lastName;
private String fullName;
private String mail;
private String homePostalAddress;
private String mobile;
private String homePhone;
private Date dateOfBirth;
private Date dateOfJoining;
private Date dateOfRelease;
private boolean active;
private String role;
private Set<Activity> activities;
public User() {
super();
}
#NaturalId
#Column(name = "USERNAME", nullable = false)
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
#Column(name = "FIRST_NAME")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#Column(name = "LAST_NAME")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
#Column(name = "FULL_NAME")
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
#Column(name = "MAIL")
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
#Column(name = "HOME_POSTAL_ADDRESS")
public String getHomePostalAddress() {
return homePostalAddress;
}
public void setHomePostalAddress(String homePostalAddress) {
this.homePostalAddress = homePostalAddress;
}
#Column(name = "MOBILE")
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
#Column(name = "HOME_PHONE")
public String getHomePhone() {
return homePhone;
}
public void setHomePhone(String homePhone) {
this.homePhone = homePhone;
}
#Column(name = "DATE_OF_BIRTH")
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
#Column(name = "DATE_OF_JOINING")
public Date getDateOfJoining() {
return dateOfJoining;
}
public void setDateOfJoining(Date dateOfJoining) {
this.dateOfJoining = dateOfJoining;
}
#Column(name = "DATE_OF_RELEASE")
public Date getDateOfRelease() {
return dateOfRelease;
}
public void setDateOfRelease(Date dateOfRelease) {
this.dateOfRelease = dateOfRelease;
}
#Column(name = "ACTIVE", nullable = false)
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
#Column(name = "ROLE")
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
#ManyToMany(cascade = { CascadeType.ALL }, mappedBy = "users", targetEntity = Activity.class)
public Set<Activity> getActivities() {
return activities;
}
public void setActivities(Set<Activity> activities) {
this.activities = activities;
}
}
Hibernate doesn't do the sorting. The database does. Executing a criteria query boils down to executing a SQL query having an order by fullName clause.
So check the configuration of your database to know how to specify the collation used by a table or column.
ALTER DATABASE adb DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
did the job.
Related
I'm trying to save an object in my MySQL DB by using Spring JPA.
That's the error I get:
[nio-8088-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolved
[org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction]
That's where I'm trying to save my object:
#Override
public RegistrationV1Response registerUser(#Valid RegistrationV1Request registrationV1Request) {
User user = new User();
user.setFirstname(registrationV1Request.getFirstname());
user.setLastname(registrationV1Request.getLastname());
user.setUsername(registrationV1Request.getUsername());
user.setEmail(registrationV1Request.getEmail());
user.setPassword(new BCryptPasswordEncoder().encode(registrationV1Request.getPassword()));
user.setActive(true);
userRepository.save(user);
return new RegistrationV1Response().withUser(user);
}
The UserRepository:
#Repository
public interface UserRepository extends CrudRepository<User, Long> {
public User findFirstByUsername(String username);
}
The User class:
#javax.persistence.Entity
#Table(name = "users")
public class User extends Entity {
#Column(name = "firstname", nullable = false, length = 100)
#NotEmpty()
private String firstname;
#Column(name = "lastname", nullable = false, length = 100)
#NotEmpty()
private String lastname;
#Column(name = "password", nullable = false, length = 100)
#NotEmpty()
private String password;
#Column(name = "username", nullable = false, unique = true, length = 100)
#NotEmpty()
private String username;
#Column(name = "email", nullable = false, unique = true, length = 100)
#NotEmpty()
#Email()
private String email;
#Column(name = "active", nullable = false)
#NotEmpty()
private boolean active;
#Column(name = "description", nullable = true, length = 250)
private String description;
#OneToMany(targetEntity = Post.class, mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> posts;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isActive() { return active; }
public void setActive(boolean active) { this.active = active; }
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Post> getPosts() { return posts; }
public void setPosts(List<Post> posts) { this.posts = posts; }
}
The Post class:
#javax.persistence.Entity
#Table(name = "posts")
public class Post extends Entity {
#Lob
#Column(name = "text", nullable = false)
#NotEmpty()
private String text;
#ManyToOne(targetEntity = User.class)
private User user;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
Reading from the DB work's without any problems just save (insert / update) doesn't work.
I just read on a similar issue that I need to add the #Transational property to the method. I did this and it's still not working. Also the other possible solutions / statements of similar issues aren't working. They're mostly about missconfigured relations or something and that's not the problem in my case I think.
Im working on a web service using Java and spring-data-jpa. I setted different endpoints that works fine. Now I implemented logics in the post method and get return an error that I can't solve.
I use postman and when I try to make a post request with id,username,lastname and password i get an error 500 and the server return me this error:
java.sql.SQLIntegrityConstraintViolationException: Column 'encrypted_password' cannot be null
package com.nicolacannata.Appws.entity;
import javax.persistence.*;
import java.io.Serializable;
#Entity(name="users")
public class UserEntity implements Serializable{
private static final long serialVersionUID = 8076405899207283205L;
#Id
#GeneratedValue
private long id;
#Column(nullable = false)
private String userId;
#Column(nullable = false, length = 50)
private String firstName;
#Column(nullable = false, length = 50)
private String lastName;
#Column(nullable = false, length = 50)
private String email;
#Column(nullable = false)
private String encryptedPassword;
private String emailVerificationToken;
#Column(nullable = false)
private boolean emailVerificationStatus = false;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getEncryptedPassword() {
return encryptedPassword;
}
public void setEncryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
}
public String getEmailVerificationToker() {
return emailVerificationToken;
}
public void setEmailVerificationToker(String emailVerificationToker) {
this.emailVerificationToken = emailVerificationToker;
}
public boolean isEmailVerificationStatus() {
return emailVerificationStatus;
}
public void setEmailVerificationStatus(boolean emailVerificationStatus) {
this.emailVerificationStatus = emailVerificationStatus;
}
}
You declared that attribute encryptedPassword cannot be null. The exception means, that this attribute was null when you attempted to save it to database. Either you forgot to set this attribute. Or you set it (you called setter), but the new value was null. To resolve the problem, set this attribute to a not-null value before saving to DB.
You have #Column(nullable = false) for private String encryptedPassword; and you are passing only id, username, lastname and password. which breaks the condition nullable = false and results in java.sql.SQLIntegrityConstraintViolationException: Column 'encrypted_password' cannot be null
Solution
Set #Column(nullable = true)
OR
Pass the value for encryptedPassword
I'm using servlets with JPA+Hibernate). I don't understand the error, unless I've tried other solutions suggested in this forum. In fact, I don't want to store the UserAccount class as an entity; but I just want to declare it in the Utilisateur class (the Ids of the Utilisateur class are declared in the useraccount class).
My code :
#Entity
#Table(name = "utilisateur")
public class Utilisateur implements Serializable {
#Id
private UserAccount userAccount;
private Civility civility;
private Address address;
private Contact contact;
#Column(name = "sessions")
private List<String> sessions;
#Column(name = "particularRules")
private boolean particularRules;
public Utilisateur(UserAccount pAccount, Civility pCivility,
Address pAddress, Contact pContact, List<String>
pSessions,
boolean particularRules) {
this.userAccount = pAccount;
this.civility = pCivility;
this.address = pAddress;
this.contact = pContact;
this.sessions = pSessions;
this.particularRules = particularRules;
}
public Civility getCivility() {
return civility;
}
public void setCivility(Civility civility) {
this.civility = civility;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public boolean isParticularRules() {
return particularRules;
}
public void setParticularRules(boolean particularRules) {
this.particularRules = particularRules;
}
public UserAccount getUserAccount() {
return userAccount;
}
public void setUserAccount(UserAccount userAccount) {
this.userAccount = userAccount;
}
public List<String> getSessions() {
return sessions;
}
public void setSessions(List<String> sessions) {
this.sessions = sessions;
}
}
#Embeddable
#Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class UserAccount implements Serializable {
public UserAccount() {
}
public UserAccount(String pId, String pEmail, String pwsd, Date pCreaDate, Date pLastModDate) {
this.identifier = pId;
this.email = pEmail;
this.password = pwsd;
this.creationDate = pCreaDate;
this.lastModificationDate = pLastModDate;
}
#OneToOne(mappedBy = "userAccount", cascade = CascadeType.ALL,
fetch = FetchType.EAGER, orphanRemoval = true, targetEntity =
Utilisateur.class)
private Utilisateur user;
#Column(name = "creationDate")
#Temporal(javax.persistence.TemporalType.DATE)
private Date creationDate;
#Column(name = "lastModificationDate")
#Temporal(javax.persistence.TemporalType.DATE)
private Date lastModificationDate;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "identifier", nullable = false)
private String identifier;
#Column(name = "email")
private String email;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "password", nullable = false)
private String password;
public String getIdentifier() {
return identifier;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Date getLastModificationDate() {
return lastModificationDate;
}
public void setLastModificationDate(Date lastModificationDate) {
this.lastModificationDate = lastModificationDate;
}
public Utilisateur getUser() {
return user;
}
public void setUser(Utilisateur user) {
this.user = user;
}
}
You must use an Embedded Primary Key.
See the answer to this question here in Stackoverflow How to create and handle composite primary key in JPA.
Best regards!
It may occur when Embedded Primary Key contains #EmbeddedId. Sub-composite key should be annotated with #Embedded instead.
I have a user dao
#Entity
#Table(name="EBIGUSERTIM")
public class EbigUser {
private String id;
private Integer source;
private String entryscheme;
private String fullName;
private String email;
private Long flags;
private String status;
private String createdBy;
private Date createdStamp;
private String modifiedBy;
private Date modifiedStamp;
#Id
#Column(name="ID")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Id
#Column(name="SOURCE")
public Integer getSource() {
return source;
}
public void setSource(Integer source) {
this.source = source;
}
#Column(name="ENTRYSCHEME")
public String getEntryscheme() {
return entryscheme;
}
public void setEntryscheme(String entryscheme) {
this.entryscheme = entryscheme;
}
#Column(name="FULLNAME")
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
#Column(name="EMAIL")
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Column(name="FLAGS")
public Long getFlags() {
return flags;
}
public void setFlags(Long flags) {
this.flags = flags;
}
#Column(name="STATUS")
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
#Column(name="CREATEDBY")
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
#Column(name="CREATEDSTAMP")
public Date getCreatedStamp() {
return createdStamp;
}
public void setCreatedStamp(Date createdStamp) {
this.createdStamp = createdStamp;
}
#Column(name="MODIFIEDBY")
public String getModifiedBy() {
return modifiedBy;
}
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
#Column(name="MODIFIEDSTAMP")
public Date getModifiedStamp() {
return modifiedStamp;
}
public void setModifiedStamp(Date modifiedStamp) {
this.modifiedStamp = modifiedStamp;
}
i am selecting 2 rows out of the db. The sql works
select * from ebigusertim where id='blah'
It returns 2 distinct rows. When i query the data using hibernate, it appears that the object memory is not being allocated for each entry in the list. Thus, i get 2 entries in the list with the same object.
Criteria userCriteria = session.createCriteria(EbigUser.class);
userCriteria.add(Restrictions.eq("id", id));
userlist = userCriteria.list();
Why are you defining two id columns(both id and source are mapped with annotation #Id)?
#Id
#Column(name="ID")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Id
#Column(name="SOURCE")
public Integer getSource() {
return source;
}
Please remove one if it is by mistake. If both together make composite key, map them accordingly e.g.
#Embeddable
public class UserPK implements Serializable {
#Column(name = "ID", nullable = false)
private String id;
#Column(name = "SOURCE", nullable = false)
private Integer source;
.....
.....
}
Use this new class in you original class as Id as below:
#EmbeddedId
private UserPK userPK;
Hope this helps.
My project setting are Spring MVC, Hibernate 3.2.x, on MySQL DB
Getting the following error:
org.hibernate.QueryParameterException: could not locate named parameter email
Approach #1:
#Override
public Boolean isExist(String email) {
boolean flag = false;
String hql = "from com.cmgr.beans.UserAccount u where u.email = :email";
List<UserAccount> result = currentSession().createQuery(hql)
.setParameter("email", email)
.list();
UserAccount userAccount = (UserAccount)result.get(0);
if (userAccount!=null && userAccount.getEmail().equalsIgnoreCase(email)) {
flag = true;
}
return flag;
}
Approach #2:
#Override
public Boolean isExist(String email) {
boolean flag = false;
String hql = "from com.cmgr.beans.UserAccount u where u.email = :email";
List<UserAccount> result = currentSession().createQuery(hql).setString("email", email).list();
UserAccount userAccount = (UserAccount) result.get(0);
if (userAccount != null && userAccount.getEmail().equalsIgnoreCase(email)) {
flag = true;
}
return flag;
}
Error:
java.lang.IllegalArgumentException: Parameter email does not exist as a named parameter in [from com.cmgr.beans.UserAccount u where u.email = :email]
UserAccount class:
package com.cmgr.beans;
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import javax.persistence.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
#Entity
#Table(name = "user_account")
public class UserAccount implements Serializable {
#Autowired
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY, generator = "user_account_seq")
#SequenceGenerator(name = "user_account_seq", sequenceName = "user_account_seq")
#Column(name = "user_id")
private Long UserId = null;
//
#Autowired
#Column(name = "user_name")
private String UserName;
//
#Autowired
#Column(name = "user_type")
private String UserType = null;
//
#Autowired
#Column(name = "first_name")
private String FirstName;
//
#Autowired
#Column(name = "last_name")
private String LastName;
//
#Autowired
#Column(name = "email")
private String Email;
//
#Autowired
#Column(name = "phone_contact_1")
private String PhoneContact1 = null;
//
#Autowired
#Column(name = "phone_contact_2")
private String PhoneContact2 = null;
//primary_address_is_usa
#Autowired
#Column(name = "primary_address_is_usa")
private Boolean primaryAddressIsUsa = null;
//
#Autowired
#Column(name = "address_1")
private String Address1 = null;
//
#Autowired
#Column(name = "city1")
private String city1 = null;
//
#Autowired
#Column(name = "state1")
private String state1 = null;
//
#Autowired
#Column(name = "country1")
private String country1 = null;
//
#Autowired
#Column(name = "zipcode")
private Integer zipcode = 0;
//
#Autowired
#Column(name = "Industry")
private String Industry = null;
//is the user account Active either due to user deactivation,admin deactivation, or nonpayment
#Autowired
#Column(name = "active")
private boolean Active = false;
//1 to 1 relation with registerationCode in Registeration class
#Autowired
#Qualifier("UserRegisteration")
#OneToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "Registeration_Code_fk", referencedColumnName = "registeration_code", nullable = false)
private UserRegisteration UserRegisteration;
//1 to many relation with EmailId in Email class
#OneToMany(cascade = {CascadeType.ALL})
#JoinColumn(name = "emailed_id")
private List<Emailed> emailed = null;
//1 to many relation with signatureId in signature class
#OneToMany(cascade = {CascadeType.ALL})
#JoinColumn(name = "signature_id")
private List<Signature> signatures;
//1 to many relation with UserAccountDocId in UserAccountDoc class
#OneToMany(cascade = {CascadeType.ALL})
#JoinColumn(name = "user_doc_id")
private Set<UserAccountDocumentRelationship> UserAccountDocumentRelationship;
#Autowired(required = false)
public UserAccount() {
}
#Autowired(required = true)
public UserAccount(String UserName, String FirstName, String LastName, String Email, String Industry) {
this.FirstName = FirstName;
this.LastName = LastName;
this.Email = Email;
this.Industry = Industry;
this.UserName = UserName;
}
#Autowired(required = false)
public UserAccount(String UserName, Long UserId, String FirstName, String LastName, String Email, String Industry) {
this.UserId = UserId;
this.FirstName = FirstName;
this.LastName = LastName;
this.Email = Email;
this.Industry = Industry;
this.UserName = UserName;
}
public String getIndustry() {
return Industry;
}
public void setIndustry(String Industry) {
this.Industry = Industry;
}
public String getAddress1() {
return Address1;
}
public void setAddress1(String Address1) {
this.Address1 = Address1;
}
public String getPhoneContact1() {
return PhoneContact1;
}
public void setPhoneContact1(String PhoneContact1) {
this.PhoneContact1 = PhoneContact1;
}
public String getPhoneContact2() {
return PhoneContact2;
}
public void setPhoneContact2(String PhoneContact2) {
this.PhoneContact2 = PhoneContact2;
}
public boolean isActive() {
return Active;
}
public void setActive(boolean Active) {
this.Active = Active;
}
public String getEmail() {
return Email;
}
public void setEmail(String Email) {
this.Email = Email;
}
public String getFirstName() {
return FirstName;
}
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
public com.cmgr.beans.UserRegisteration getUserRegisteration() {
return UserRegisteration;
}
public void setUserRegisteration(com.cmgr.beans.UserRegisteration UserRegisteration) {
this.UserRegisteration = UserRegisteration;
}
public Long getUserId() {
return UserId;
}
public void setUserId(Long UserId) {
this.UserId = UserId;
}
public String getUserType() {
return UserType;
}
public void setUserType(String UserType) {
this.UserType = UserType;
}
public List<Emailed> getEmailed() {
return emailed;
}
public void setEmailed(List<Emailed> emailed) {
this.emailed = emailed;
}
public List<Signature> getSignatures() {
return signatures;
}
public void setSignatures(List<Signature> signatures) {
this.signatures = signatures;
}
public String getCity1() {
return city1;
}
public void setCity1(String city1) {
this.city1 = city1;
}
public String getCountry1() {
return country1;
}
public void setCountry1(String country1) {
this.country1 = country1;
}
public Boolean getPrimaryAddressIsUsa() {
return primaryAddressIsUsa;
}
public void setPrimaryAddressIsUsa(Boolean primaryAddressIsUsa) {
this.primaryAddressIsUsa = primaryAddressIsUsa;
}
public String getState1() {
return state1;
}
public void setState1(String state1) {
this.state1 = state1;
}
public Integer getZipcode() {
return zipcode;
}
public void setZipcode(Integer zipcode) {
this.zipcode = zipcode;
}
public String getUserName() {
return UserName;
}
public void setUserName(String UserName) {
this.UserName = UserName;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UserAccount other = (UserAccount) obj;
if ((this.UserId == null) ? (other.UserId != null) : !this.UserId.equals(other.UserId)) {
return false;
}
return true;
}
#Override
public int hashCode() {
int hash = 3;
hash = 73 * hash + (this.UserId != null ? this.UserId.hashCode() : 0);
return hash;
}
public Set<com.cmgr.beans.UserAccountDocumentRelationship> getUserAccountDocumentRelationship() {
return UserAccountDocumentRelationship;
}
public void setUserAccountDocumentRelationship(Set<com.cmgr.beans.UserAccountDocumentRelationship> UserAccountDocumentRelationship) {
this.UserAccountDocumentRelationship = UserAccountDocumentRelationship;
}
}
From what i remember, this is a case of Hibernate reporting the wrong error message. I guess, the actual error is "mapping not found for com.cmgr.beans.UserAccount". Try this query:
String hql = "from com.cmgr.beans.UserAccount";
This probably will show you the correct error message. And once you fix that, you can change it to accept the parameters.
Today i had a similar issue...
My entity was not in the scanned packages from Spring. So Hibernate somehow did something but the error message was quite confusing and did not really fit to the real issue.
To solve my problem i had to add the entities to the packagescan.
Change your query to
String hql = "from com.cmgr.beans.UserAccount u where u.Email = :email";
Since your UserAccount class has a property of Email
Hibernate somewhy throws different exception than it has to be.. Probably by correcting this you should get rid of the problem:
Rename fields in the class to follow JavaConventions (should start with a small letter)
Use simple class name instead of fully qualified
I see that in the UserAccount class, the property for the email address is defined as 'Email' and not 'email'.
Refer to the hibernate documentation
It is advisable to use java naming convention hence I would advice you to name your property to 'email' in UserAccount.
replace:
#Autowired
#Column(name = "email")
private String Email;
//
with:
#Autowired
private String email;
use java naming conventions of variable naming small letters & don't need to write
#Column(name = "email")
because variable name is same name as column name
Just Try this....
In place of
List<UserAccount> result = currentSession().createQuery(hql)
.setParameter("email", email)
.list();
use
List<UserAccount> result = currentSession().createQuery(hql)
.setString("email", email)
.list();
may be it will helps you....
Verify that in the SQL query the parameter starts with ':'
Example: WHERE NAME = :pMyParameter
At the same time, you must take into account that in the Java code the parameter must not be named with ':'
Example: parameters.put("pMyParameter", myParameter);
may be it will helps you...