DB Tibero can be without PK . The bulk of the database entity in the code (generate persistence mappings by database schema) requires a primary key in Hibernate .

Of course, you can consider the ResultSet and further process it, but it is convenient when reading, and when writing or updating a record in the database, you will have to implement your bike.

What do you recommend? Are there any other ORM systems that can work without PK?

Ps without a PC, even update is impossible to do, since there is no unique field that would allow to accurately identify the record. All attributes in Tibero can be nullable, that is, you can have N entries with empty fields.

  • In fact, in virtually any database, entities, or rather tables, can be without PK, everyone can have more than 100,500 null entries. But ORM without PK does not work. It is necessary to somehow distinguish the essence of each other. - Sergey

1 answer 1

can someone come in handy ....

created an annotation to determine the primary key

 @Retention(RetentionPolicy.RUNTIME) public @interface PrimaryKey { } @PrimaryKey private BigDecimal id_Row; 

I registered the address in each class

 public String getPath(){ return "s.table"; } 

I receive ResultSet and I transfer in a method for class filling

  /** * Load data from DB to collection by mapping fields from class type */ public Object loadObjectFromResultSet(Class classType, ResultSet resultSet) throws Exception { ArrayList<Object> objectArrayList = new ArrayList<>(); Object fieldValue; while(resultSet.next()) { Object typeOfFieldsClass = classType.newInstance(); Field[] classFields = typeOfFieldsClass.getClass().getFields(); for (Field field : classFields) { fieldValue = resultSet.getObject(field.getName()); field.set(typeOfFieldsClass, fieldValue); } objectArrayList.add(typeOfFieldsClass); } return objectArrayList; } 

Returns List<MyEntityClass> , where I work and change what I need

  //Данный метод сохраняет все public static <T> void saveAll(List<T> objects, DataBase db) throws ReflectiveOperationException { db.connectToTibero(); for (T c : objects) { String qw = createQuery(c); System.out.println(qw); db.executeUpdate(qw); } db.executeUpdate("commit;"); } private static String createQuery(Object c) throws ReflectiveOperationException { StringBuilder sb = new StringBuilder("UPDATE "); sb.append(getDbPath(c.getClass())).append(" set "); Object clazz = c; java.lang.reflect.Field[] fields = c.getClass().getFields(); for (int i = 0; i < fields.length - 1; i++) { Object value = fields[i].get(clazz); if(value == null) value = ""; sb.append(fields[i].getName()).append(" = '").append(value).append("', "); } if (fields.length > 1) { sb.append(fields[fields.length - 1].getName()) .append(" = '") .append(fields[fields.length - 1].get(clazz)) .append("' "); Field primField = Arrays.stream(fields) .filter(field -> field.isAnnotationPresent(PrimaryKey.class)) .findFirst() .orElseThrow(() -> new NotFoundException("cannot found public field with annotation @PrimaryKey")); sb.append(" where " ) .append(primField.getName()) .append(" = '") .append(primField.get(clazz)) .append("' ;"); return sb.toString(); } else { return null; } /** * Get path for table from method declared in base class type * * @return string with table path */ private static String getDbPath(Class classType) throws ReflectiveOperationException { Object value = null; Object tmpObject = null; tmpObject = classType.newInstance(); java.lang.reflect.Method method; if (tmpObject != null) { method = classType.getMethod("getPath"); value = method.invoke(tmpObject); } else { throw new RuntimeException("Empty object"); } return (String) value; }