2015-07-03 73 views
4

我試圖修復由我的數據庫上的Hibernate查詢導致的錯誤,但我似乎無法找到查詢來自哪裏。啓用sql登錄休眠後,我發現錯誤在哪裏,但不知道如何解決。如何查找/更改休眠查詢

Hibernate query(eclipse log) 「update students_classes set student_id = null where student_id =?」

投擲: ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper](HTTP-本地主機 - 127.0.0.1-8080-2)柱 'student_id數據' 不能爲空

的錯誤是在這行拋出:

student = studentDAO.save(student); 

其中保存來自

public Entity save(Entity entity) throws Exception { 
     Entity result = null; 
     try { 
      trimAllStrAttributes(entity); 
      result = em.merge(entity); 
     } catch (Exception e) { 
      logger.error("Exception in AbstractDAO", e); 
      throw e; 
     } 

     return result; 
    } 


private void trimAllStrAttributes(Entity product) throws IntrospectionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { 
     final Class c = product.getClass(); 
     for (PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(c, Object.class).getPropertyDescriptors()) { 
      Method method = propertyDescriptor.getReadMethod(); 
      if (method != null) { 
       String name = method.getName(); 

       // If the current level of Property is of type String 
       if (method.getReturnType().equals(String.class)) { 
        String property = (String) method.invoke(product); 
        if (property != null) { 
         try { 
          Method setter = c.getMethod("set" + name.substring(3), new Class<?>[] { String.class }); 
          if (setter != null) { 
           setter.invoke(product, property.trim()); 
          } 
         } catch (NoSuchMethodException nsme) { 
         } 
        } 
       } 
      } 
     } 
    } 

它可以是一個映射問題,所以這裏是我的恩tities:

StudentClasses

@Column(name = "student_id") 
private Long studentId; 

@Column(name = "classes_id") 
private Long classesId; 

學生

@AuditJoinTable 
    @ManyToMany 
    @JoinTable(name = "students_classes", 
    joinColumns = @JoinColumn(name = "student_id", referencedColumnName = "id"), 
    inverseJoinColumns = @JoinColumn(name = "classes_id", referencedColumnName = "id")) 
    private List<Classes> classes; 

@NotAudited 
@OneToMany 
@JoinColumn(name = "student_id") 
private List<StudentClasses> studentsClasses; 

我應該怎麼辦?改變hibernate的查詢(在哪裏找到它?)還是在映射級別有問題?

+0

您應該包括在問題的堆棧跟蹤 – meskobalazs

+0

你在做一個'setStudentClasses(newClasses)'在你的代碼,更換託管集合?什麼是'trimAllStrAttbributes'在做什麼? –

+0

stacktrace的相關部分已經在這裏@meskobalazs。我編輯了新的方法M. Deinum –

回答

2

錯誤解釋本身:

投擲:ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper](HTTP-本地主機 - 127.0.0。1-8080-2)列「student_id數據」不能爲空

按照您的其他映射,我可以肯定地說studentId是表的主鍵,所以,如果你不知道ID新的實體必須有,必須標記領域,使休眠圖吧:

馬克studentId爲id並添加一個自動生成的值:

@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
private Long studentId; 

Hibernate定義了五種類型的標識符生成策略:

  • AUTO - 取決於底層DB在任標識列,序列或表
  • - 表保存的ID
  • IDENTITY - 標識列
  • SEQUENCE - 序列
  • 身份複製 - 身份從另一個實體複製
+0

它沒有工作,同樣的錯誤出現@Jordi –

0

我的第一個猜測是,在這條線

result = em.merge(entity); 

entity對象沒有一套studentId屬性,一個是導致異常。所以你需要自己設置它,或者自動生成它,如其他答案所示。

1

問題在於StudentId主鍵的序列創建配置。你必須在這裏做兩件事。

  1. 在像下面的數據庫創建序列..

    CREATE SEQUENCE STUDENT_SEQ 
        MINVALUE 1 
        MAXVALUE 999999999999999999999999999 
        START WITH 1 
        INCREMENT BY 1 
        CACHE 20; 
    
  2. 在Hibernate配置中指定的序列名稱。

    @Id 
        @SequenceGenerator(name = "StudentSeq", sequenceName="STUDENT_SEQ", allocationSize=1) 
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "StudentSeq") 
        @Column(name = "Student_id", nullable = false) 
        private Long StudentId;