Announcement Announcement Module
Collapse
No announcement yet.
Cannot insert null Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cannot insert null

    We have a tested app that is working fine. When I send it to the client I suddenly start getting (MS SQL Server):

    Code:
    Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'UUID', table 'AgileJury.dbo.AUDIT'; column does not allow nulls. INSERT fails.
    	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
    	at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
    	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:390)
    	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:340)
    	at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4575)
    	at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
    	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
    	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
    	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:308)
    	at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
    	at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
    	at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
    	... 53 more

    The annotations for this column are set up in the super class as follows:

    Code:
      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      @Column(name = COLUMN_UUID, nullable = false, unique = true)
      protected Long uuid = EMPTYUUID;
    Any ideas?

    Thanks.

  • #2
    Could you resolve this problem?

    Comment


    • #3
      Originally posted by thehl View Post
      Code:
        @Id
        @GeneratedValue(strategy=GenerationType.AUTO)
        @Column(name = COLUMN_UUID, nullable = false, unique = true)
        protected Long uuid = EMPTYUUID;
      Any ideas?

      First are you sure your SQL table has the "IDENTITY" flag set for the UUID column ? Using SQL to INSERT a NULL can be a correct behavor to cause an IDENTITY value to be generated by the SQL server. MySQL users will know of IDENTITY as AUTO_INCREMENT. Another valid stratagy is to omit the UUID column from the INSERT completely.

      Have you checked the SQL server dialect hibernate detects is the correct one, this will show up in the SessionFactory creation log output.


      Failing the above concerns...

      Change the type from "Long" to "long" in the entity, this is something worth considering if a valid is a primative type and cannot be null why use the boxed form?

      What is EMPTYUUID set to ? Hibernate may require you to advise it explicitly if you use a non-standard value (non-standard would be something other than "null" or "0").

      I would have thought the JPA stratagy for @Id would implicitly mean nullable=false, which you reinforce in @Column.



      Also a question from me... why use the UUID name and not "id" ? To me a UUID is longer a binary key.

      Comment


      • #4
        Umm

        It's been a while, but I use Hypersonic for testing and MS SQL for production and I think I had the wrong dialect and that caused the issue.

        UUID was chosen by another project and we copied it. It is, in fact, not a universal unique ID, but only unique for the column. I would've used ID, but the standard was already set and I try not to fight about small things so that I can be more effective fighting the big ones.

        Comment


        • #5
          Hello

          Two observations, according with any book about Database, has no sense have a PK with null value, what special type of requirement ask this approach?

          Even if is mandatory has a null value, you have this

          Code:
          @Id
            @GeneratedValue(strategy=GenerationType.AUTO)
            @Column(name = COLUMN_UUID, nullable = false, unique = true)
            protected Long uuid = EMPTYUUID;
          try with nullable = true, I dont kwow if this could work, even when you set the value to true, for example MySQL request always not null values for a PK

          Comment


          • #6
            Thanks for the answers, it was a silly problem because I'm a new user of MS SQL. I didn't define the primary key with the IDENTITY flag as dlmiles said.

            By the way, do you know if this is the best way to define a column as autoincrement?

            Comment


            • #7
              Originally posted by migelct1983 View Post
              By the way, do you know if this is the best way to define a column as autoincrement?
              For MSSQL yes that is the best/correct (maybe only) way.

              Comment


              • #8
                If you use annotation it will be difficult to put null value IN Hibernateutil file remove annotation configuration use as given below


                import org.hibernate.*;
                import org.hibernate.cfg.*;

                public class HibernateUtil {

                private static final SessionFactory sessionFactory;

                static {
                try {
                // Create the SessionFactory from hibernate.cfg.xml
                sessionFactory = new Configuration().configure().buildSessionFactory();
                } catch (Throwable ex) {
                // Make sure you log the exception, as it might be swallowed
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
                }
                }

                public static SessionFactory getSessionFactory() {
                return sessionFactory;
                }

                }


                use bean class corrrectly to add values to database in mapping files .

                In mysql give null properties for the values.

                Comment

                Working...
                X