**This is student pojo class**
@Entity
@Table(name="student")
public class Student {
@Id
private int studentId;
private String StudentName;
@Embedded
private Adress add;
@Embedded
private Adress temp;
public void setTemp(Adress temp) {
this.temp = temp;
}
public void setAdd(Adress add) {
this.add = add;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public void setStudentName(String studentName) {
StudentName = studentName;
}}
** this is Address pojo class**
@Embeddable
public class Adress {
@Column(name="village" )
private String village;
@Column(name="pin")
private int pin;
public void setVillage(String village) {
this.village = village;
}
public void setPin(int pin) {
this.pin = pin;
}}
** Main class **
public class Test {
public static void main(String[] args) {
SessionFactory factory=Sessionfactory.getfactory();
Session session=factory.openSession();
Adress a=new Adress();
a.setPin(123);
a.setVillage("kanipakam");
Adress a2=new Adress();
a2.setVillage("kanchi");
a2.setPin(1234);
Student s=new Student();
s.setAdd(a);
s.setTemp(a2);
s.setStudentId(101);
s.setStudentName("uday");
Transaction tx=session.beginTransaction();
session.save(s);
tx.commit();
System.out.println("object saved");
session.close();
factory.close();
}}
** showing error**
运行此代码时显示如下错误请帮助我 线程" main"中的例外情况org.hibernate.MappingException:实体映射中的重复列:com.sathya.pojo.Student column:pin(应使用insert =&#34映射; false" update =" false") 在org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:709) 在org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:731) 在org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:727) 在org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:753) 在org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:506) 在org.hibernate.mapping.RootClass.validate(RootClass.java:270) 在org.hibernate.cfg.Configuration.validate(Configuration.java:1358) 在org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849) 在com.sathya.singleton.Sessionfactory.getfactory(Sessionfactory.java:21) 在com.sathya.Test.Test.main(Test.java:16)
答案 0 :(得分:0)
@Embedded
注释指定对象中的字段与根实体保持在同一个表中。列的名称取自@Embeddable
配置。所以在你的情况下
@Embedded
private Adress add;
@Embedded
private Adress temp;
指定Adress
上有2个@Entity
类型的对象。问题是,您当前的配置add.village
和temp.village
都将映射到village
表的student
列。
您有2个选项可以解决此问题:
@Embedded
个属性的映射,并指定不同的列,如所述here many-to-one
或one-to-one
关系在单独的数据库表中寻址和存储地址记录。