我是hibernate的新手。我正在使用hibernate将数据插入到数据库中。将数据插入数据库时会出现“无效列索引”错误。我在hibernate中使用OnetoMany映射。如果我在hibernate中执行onetomany映射时遇到了错误,请告诉我。请帮帮我。任何帮助都真的很有意义。请参考下面的代码实体类 -
家长班 - >
@Entity
@Table(name = "Parent_Table")
public class ParentEntity implements Serializable{
private static final long serialVersionUID = 4805702152070138493L;
private long my_Id;
private String my_Name;
private long locNbr;
private Timestamp createTs;
private String createUsrId;
private Timestamp lstUpdTs;
private String lstUpdUsrId;
private Integer version;
private List<ChildEntity> childInfo = new ArrayList<ChildEntity>(0);
@Id
@SequenceGenerator(name = "MY_ID_GENERATOR", sequenceName = "MY_ID_SEQ")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "MY_ID_GENERATOR")
@Column(name = "MY_ID", unique = true, nullable = false)
public long getMyId() {
return my_Id;
}
public void setMyId(long my_Id) {
this.my_Id = my_Id;
}
@Column(name = "MY_NAME", nullable = false)
public String getMy_Name() {
return my_Name;
}
public void setMy_Name(String my_Name) {
this.my_Name = my_Name;
}
@Column(name = "LOC_NBR", nullable = false)
public long getLocNbr() {
return locNbr;
}
public void setLocNbr(long locNbr) {
this.locNbr = locNbr;
}
@Column(name = "CREATE_TS", nullable = false)
public Timestamp getCreateTs() {
return createTs;
}
public void setCreateTs(Timestamp createTs) {
this.createTs = createTs;
}
@Column(name = "CREATE_USR_ID", nullable = false)
public String getCreateUsrId() {
return createUsrId;
}
public void setCreateUsrId(String createUsrId) {
this.createUsrId = createUsrId;
}
@Column(name = "LST_UPD_TS", nullable = false)
public Timestamp getLstUpdTs() {
return lstUpdTs;
}
public void setLstUpdTs(Timestamp lstUpdTs) {
this.lstUpdTs = lstUpdTs;
}
@Column(name = "LST_UPD_USR_ID", nullable = false)
public String getLstUpdUsrId() {
return lstUpdUsrId;
}
public void setLstUpdUsrId(String lstUpdUsrId) {
this.lstUpdUsrId = lstUpdUsrId;
}
@Column(name = "VERSION")
@Version
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "MY_ID", nullable = false)
public List<ChildEntity> getchildInfo() {
return childInfo;
}
public void setchildInfo(List<ChildEntity> childInfo) {
this.childInfo = childInfo;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ParentEntity other = (ParentEntity) obj;
if (my_Id != other.my_Id)
return false;
if (my_Name == null) {
if (other.my_Name != null)
return false;
}else if (!my_Name.equals(other.my_Name))
return false;
if(childInfo == null){
if(other.childInfo != null)
return false;
}else if (!childInfo.equals(other.childInfo))
return false;
if (locNbr != other.locNbr)
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (my_Id ^ (my_Id >>> 32));
result = prime * result + (int) (locNbr ^ (locNbr >>> 32));
result = prime * result + ((my_Name == null) ? 0 : my_Name.hashCode());
result = prime * result + ((childInfo == null) ? 0 : childInfo.hashCode());
return result;
}
}
儿童类 - &gt;
@Entity
@Table(name = "Child_Table")
@IdClass(ChildEntityPK.class)
public class ChildEntity implements Serializable{
private static final long serialVersionUID = 4195294854422389625L;
private long my_Id;
private long deptNbr;
private long locNbr;
private Timestamp createTs;
private String createUsrId;
private Integer version;
private ParentEntity parent;
@Id
@Column(name = "MY_ID", nullable = false, insertable = false, updatable = false)
public long getMyId() {
return my_Id;
}
public void setMyId(long my_Id) {
this.my_Id = my_Id;
}
@Id
@Column(name = "DEPT_NBR", nullable = false)
public long getDeptNbr() {
return deptNbr;
}
public void setDeptNbr(long deptNbr) {
this.deptNbr = deptNbr;
}
@Column(name = "LOC_NBR", nullable = false)
public long getLocNbr() {
return locNbr;
}
public void setLocNbr(long locNbr) {
this.locNbr = locNbr;
}
@Column(name = "CREATE_TS", nullable = false)
public Timestamp getCreateTs() {
return createTs;
}
public void setCreateTs(Timestamp createTs) {
this.createTs = createTs;
}
@Column(name = "CREATE_USR_ID", nullable = false)
public String getCreateUsrId() {
return createUsrId;
}
public void setCreateUsrId(String createUsrId) {
this.createUsrId = createUsrId;
}
@Column(name = "VERSION")
@Version
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "MY_ID", referencedColumnName = "MY_ID", nullable = false, insertable = false, updatable = false)
public ParentEntity getParent() {
return parent;
}
public void setParent(ParentEntity parent) {
this.parent = parent;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ChildEntity other = (ChildEntity) obj;
if (my_Id != other.my_Id)
return false;
if (deptNbr != other.deptNbr)
return false;
if (locNbr != other.locNbr)
return false;
if (parent == null) {
if (other.parent != null)
return false;
} else if (!parent.equals(other.parent))
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (my_Id ^ (my_Id >>> 32));
result = prime * result + (int) (deptNbr ^ (deptNbr >>> 32));
result = prime * result + (int) (locNbr ^ (locNbr >>> 32));
result = prime * result + ((parent == null) ? 0 : parent.hashCode());
return result;
}
}
ChildPk类 -
public class ChildEntityPK implements Serializable{
private static final long serialVersionUID = 5118485760469581332L;
private long my_Id;
private long deptNbr;
@Id
@Column(name = "MY_ID", nullable = false, insertable = false, updatable = false)
public long getMy_Id) {
return my_Id;
}
public void setMy_Id(long my_Id) {
this.my_Id = my_Id;
}
@Id
@Column(name = "DEPT_NBR", nullable = false)
public long getDeptNbr() {
return deptNbr;
}
public void setDeptNbr(long deptNbr) {
this.deptNbr = deptNbr;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ChildEntityPK other = (ChildEntityPK) obj;
if (my_Id != other.my_Id)
return false;
if (deptNbr != other.deptNbr)
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (my_Id ^ (my_Id >>> 32));
result = prime * result + (int) (deptNbr ^ (deptNbr >>> 32));
return result;
}
}
错误 -
Caused by: java.sql.SQLException: Invalid column index
at oracle.jdbc.driver.OraclePreparedStatement.setLongInternal(OraclePreparedStatement.java:4936) [ojdbc6-11.2.0.3.jar:11.2.0.3.0]
at oracle.jdbc.driver.OraclePreparedStatement.setLong(OraclePreparedStatement.java:4923) [ojdbc6-11.2.0.3.jar:11.2.0.3.0]
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setLong(OraclePreparedStatementWrapper.java:210) [ojdbc6-11.2.0.3.jar:11.2.0.3.0]
at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.setLong(WrappedPreparedStatement.java:229)
at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$1.doBind(BigIntTypeDescriptor.java:57) [hibernate-core-4.2.18.Final-redhat-2.jar:4.2.18.Final-redhat-2]
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:93) [hibernate-core-4.2.18.Final-redhat-2.jar:4.2.18.Final-redhat-2]
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:284) [hibernate-core-4.2.18.Final-redhat-2.jar:4.2.18.Final-redhat-2]
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:279) [hibernate-core-4.2.18.Final-redhat-2.jar:4.2.18.Final-redhat-2]
at org.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:343) [hibernate-core-4.2.18.Final-redhat-2.jar:4.2.18.Final-redhat-2]
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrateId(AbstractEntityPersister.java:2835) [hibernate-core-4.2.18.Final-redhat-2.jar:4.2.18.Final-redhat-2]
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2804) [hibernate-core-4.2.18.Final-redhat-2.jar:4.2.18.Final-redhat-2]
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3076) [hibernate-core-4.2.18.Final-redhat-2.jar:4.2.18.Final-redhat-2]
... 91 more