@Entity
@IdClass(GirlPK.class)
public class Girl{
private int id;
private String name;
private int age;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Id
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class GirlPK implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean equal(Object obj){
if(this == obj){
return true;
}
if(obj != null && obj.getClass() == GirlPK.class){
GirlPK target = (GirlPK)obj;
if(target.getId() == getId() && target.getName().equals(getName())){
return true;
}
}
return false;
}
public int hashCode(){
return getId()*7+getName().hashCode();
}
public class Test {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static void main(String[] args) {
GirlPK gpk = new GirlPK();
Girl g = new Girl();
g.setAge(23);
Configuration config = new Configuration().configure(); // 读取hibernate配置文件
serviceRegistry = new ServiceRegistryBuilder().applySettings(
config.getProperties()).buildServiceRegistry();
sessionFactory = config.buildSessionFactory(serviceRegistry); // 创建SessionFactory
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(g);
session.flush();
session.getTransaction().commit();
}
}
如代码所示
GirlPK为复合主键
Girl为实体类
用hibernate自动生成表显示的SQL为
drop table Girl
create table Girl (
id int identity not null,
name varchar(255) not null,
age int not null,
primary key (id, name)
)
但是之后执行test的时候报错为
ERROR: HHH000123: IllegalArgumentException in class: test.GirlPK, setter method of property: id
二月 10, 2012 9:21:32 下午 org.hibernate.property.BasicPropertyAccessor$BasicSetter set
ERROR: HHH000091: Expected type: int, actual value: org.hibernate.id.IdentifierGeneratorHelper$2
Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of test.GirlPK.id
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:119)
at org.hibernate.mapping.Component$ValueGenerationPlan.execute(Component.java:436)
at org.hibernate.id.CompositeNestedGeneratedValueGenerator.generate(CompositeNestedGeneratedValueGenerator.java:121)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:120)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:204)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:189)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:642)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:635)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:631)
at test.Test.main(Test.java:25)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.property.BasicPropertyAccessor$BasicSetter.set(BasicPropertyAccessor.java:65)
... 12 more
大概意思好像就是和GirlPK的id的类型不匹配
小弟实在是没明白是怎么个不匹配
其实目的就是想在那个复合主键里面让id为自动生成,name为自己填写
求大大们帮忙看看怎样解决呗
2 个解决方案
#1
csdn的大神哥哥们,就没人能帮帮小弟嘛
#2
好像不能这样做吧 。。 你在哪里看 的饿 。。 我以前做 都是 先把这2个属性封装成一个对象,让id为主键。然后把这个对象当作一个主键来使用!!
#1
csdn的大神哥哥们,就没人能帮帮小弟嘛
#2
好像不能这样做吧 。。 你在哪里看 的饿 。。 我以前做 都是 先把这2个属性封装成一个对象,让id为主键。然后把这个对象当作一个主键来使用!!