当我尝试在休眠状态下检索一个对象时,它会抛出类型不匹配异常。
HTTP Status 500 - Request processing failed; nested exception is
org.hibernate.TypeMismatchException: Provided id of the wrong type for class
com.ecommerce.models.User. Expected: class com.ecommerce.models.User, got class
java.lang.Integer
Session session=getSessionFactory().openSession();
Transaction tx;
tx=session.beginTransaction();
User u1=new User();
u1.setUserName("abcdef");
u1.setPassword("abcdef");
u1.setEmailId("abcdef");
session.save(u1);
tx.commit();
User u=(User)session.get(User.class,u1.getId()); -->this line is throwing error
班级用户
@Entity
@Table(name="USER")
public class User implements Serializable {
@Id
@GenericGenerator(name="gen",strategy="increment")
@GeneratedValue(generator="gen")
@NotNull
int Id;
@Id
@Column(name="USERNAME")
@NotEmpty
String userName;
@Id
@Column(name="EMAIL_ID")
@NotEmpty
String emailId;
@Column(name="PASSWORD")
@NotEmpty
String password;
Seetters and getters.....
}
有人可以帮忙吗?
答案 0 :(得分:0)
您有三个用@Id注释的字段。如果您使用代理自动生成的密钥,则从电子邮件和用户名中删除@Id。否则,如果您使用电子邮件和用户名作为复合PK,那么您不需要自动生成代理键,因此请删除它并查看如何使用复合ID类:
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Composite_Primary_Keys
答案 1 :(得分:0)
您将在课堂上使用@Embeddeble
和@EmbeddedId
并指定pk列。有关详情,请查看here