Всем привет. В бд есть следующий тип:
create type p_records as object
(name varchar2(50),"sum" number,cur number);
и таблица которая его использует:
CREATE TABLE test_collections(
t_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
obj p_records,
PRIMARY KEY(t_id));
С помощью hibernate пытаюсь получить список всех объектов из этой таблице.
Query query = session.createQuery("from Test_collections");
List<Obj> objs = query.getResultList();
Но вместо списка получаю ошибку.
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Hibernate: select test_colle0_.t_id as t_id1_0_, test_colle0_.NAME as name2_0_, test_colle0_.sum as sum3_0_, test_colle0_.cur as cur4_0_ from test_collections test_colle0_
июн 08, 2022 1:29:52 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 904, SQLState: 42000
июн 08, 2022 1:29:52 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-00904: "TEST_COLLE0_"."CUR": недопустимый идентификатор
у меня не правильно строиться запрос. Не могу понять как это исправить.
Класс для типа p_records выглядит так:
package types;
import java.io.Serializable;
public class Obj implements Serializable {
private String name;
private Integer sum;
private Integer cur;
public Obj(){};
public Obj(String name,int sum,int cur){
this.name = name;
this.sum = sum;
this.cur = cur;
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCur() {
return cur;
}
public void setCur(Integer cur) {
this.cur = cur;
}
public Integer getSum() {
return sum;
}
public void setSum(Integer sum) {
this.sum = sum;
}
}
Класс который описывает тип:
package types;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.*;
import org.hibernate.usertype.CompositeUserType;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Objects;
public class ObjType implements CompositeUserType
{
@Override
public String[] getPropertyNames() {
return new String[]{"name","sum","cur"};
}
@Override
public Type[] getPropertyTypes() {
return new Type[]{StringType.INSTANCE, IntegerType.INSTANCE,IntegerType.INSTANCE};
//return new Type[]{StandardBasicTypes.STRING,StandardBasicTypes.INTEGER,StandardBasicTypes.INTEGER};
}
@Override
public Object getPropertyValue(Object o, int i) throws HibernateException {
Obj obj = (Obj) o;
switch (i){
case 0:
return obj.getName();
case 1:
return obj.getSum();
case 2:
return obj.getCur();
}
throw new IllegalArgumentException(o +
" is an invalid property index for class type " +
obj.getClass().getName());
}
@Override
public void setPropertyValue(Object o, int i, Object o1) throws HibernateException {
Obj obj = (Obj) o;
switch (i){
case 0:
obj.setName((String) o1);
case 1:
obj.setSum((Integer) o1);
case 2:
obj.setCur((Integer) o1);
throw new IllegalArgumentException(o + "is a invalid property for class type " +
obj.getClass().getName());
}
}
@Override
public Class returnedClass() {
return Obj.class;
}
@Override
public boolean equals(Object o, Object o1) throws HibernateException {
if(o == o1) return true;
if(Objects.isNull(o) || Objects.isNull(o1)) return false;
return o.equals(o1);
}
@Override
public int hashCode(Object o) throws HibernateException {
return o.hashCode();
}
@Override
public Object nullSafeGet(ResultSet resultSet, String[] strings, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException, SQLException {
if(resultSet.wasNull()) return null;
Obj obj = new Obj();
obj.setName(resultSet.getString(strings[0]));
obj.setSum(resultSet.getInt(strings[1]));
obj.setCur(resultSet.getInt(strings[2]));
return obj;
}
@Override
public void nullSafeSet(PreparedStatement preparedStatement, Object o, int i, SharedSessionContractImplementor sharedSessionContractImplementor) throws HibernateException, SQLException {
if(Objects.isNull(o)){
preparedStatement.setNull(i, Types.VARCHAR);
}else{
Obj obj = (Obj) o;
preparedStatement.setString(i,obj.getName());
preparedStatement.setInt(i+1,obj.getSum());
preparedStatement.setInt(i+2,obj.getCur());
}
}
@Override
public Object deepCopy(Object o) throws HibernateException {
if(Objects.isNull(o)) return null;
Obj obj = (Obj)o;
Obj objForCopy = new Obj(obj.getName(),obj.getSum(), obj.getSum());
return objForCopy;
}
@Override
public boolean isMutable() {
return true;
}
@Override
public Serializable disassemble(Object o, SharedSessionContractImplementor sharedSessionContractImplementor) throws HibernateException {
return (Serializable) deepCopy(o);
}
@Override
public Object assemble(Serializable serializable, SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException {
return deepCopy(serializable);
}
@Override
public Object replace(Object o, Object o1, SharedSessionContractImplementor sharedSessionContractImplementor, Object o2) throws HibernateException {
return o;
}
}
Описание таблицы:
package entities;
import org.hibernate.annotations.Columns;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.type.CompositeType;
import types.Obj;
import types.ObjType;
import javax.persistence.*;
//@TypeDef(name = "obj", typeClass = types.ObjType.class, defaultForType = Obj.class)
@Entity
@Table(name = "test_collections")
public class Test_collections {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "t_id")
private Long t_id;
@Columns(columns = { @Column(name = "NAME"),@Column(name = "sum"), @Column(name = "cur") })
@Type(type = "types.ObjType")
private Obj obj;
public Test_collections() {
};
public Test_collections(Long t_id, Obj obj) {
this.t_id = t_id;
this.obj = obj;
};
public Long getT_id() {
return t_id;
}
public void setT_id(Long t_id) {
this.t_id = t_id;
}
public Obj getObj() {
return obj;
}
public void setObj(Obj obj) {
this.obj = obj;
}
}
Полные исходники можно посмотреть тут https://github.com/developKmv/Hibernate_tst