Announcement Announcement Module
Collapse
No announcement yet.
Why is roo/hibernate inserting null? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why is roo/hibernate inserting null?

    Hi All,
    This is my first roo project. It sure looked easier watching the videos.
    I have an Owner to persist:

    Code:
    @RooJavaBean
    @RooToString
    @RooEntity(table = "Owner", identifierField="ownerID", identifierColumn="OwnerID")
    public class Owner {
    
    	@Id
    	@Column(name="OwnerID", unique = true, nullable=false)
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long ownerID;
    Yet when I try to create one using the UI that roo generates I get this error in the console:
    Code:
    ERROR org.hibernate.util.JDBCExceptionReporter - Attempt to insert null into a non-nullable column: column: OWNERID table: OWNER in statement [insert into owner (ownerid, email, owner_name, password, password_hint, version) values (null, ?, ?, ?, ?, ?)]
    I'm saying OwnerID nullable=false and that it should be auto-generated. So why is it passing null and/or why does it care since it should provide one for me?

    Any suggestions are greatly appreciated.

    Andrew

  • #2
    Does the database have a trigger/autoincrement on this column ?

    I usually get this error when I have forgotten the autoincrement on this column ...


    Otherwise maybe try insertable=false on the column ?

    Comment


    • #3
      Thanks for the reply.

      I don't think it has any triggers. I'm using the Hibernate/HyperSonicSql until I get CRUD working for all our objects. I'm building the schema via annotations.

      Comment


      • #4
        Oh I took the other way .. generation from an existing databse ...

        But now I understand your problem ... what Database are you using ?

        Did you try GenerationType.TABLE maybe ?

        Comment


        • #5
          Originally posted by KingAndrew View Post
          Hi All,
          Code:
          @RooEntity(table = "Owner", identifierField="ownerID", identifierColumn="OwnerID")
          	@Column(name="OwnerID", unique = true, nullable=false)//use @NotNull instead
              @GeneratedValue(strategy = GenerationType.AUTO)
              private Long ownerID;
          IMHO you're over-specifying the red parts above of the @RooEntity annotation. Why not let Roo make Hibernate create table and field names directly from the Java classes? Less room for errors at this point. If you need this setup you can make it work without these first to get into the mind of roo development. Then switch to what you prefer.
          And is "ownerID" supposed to replace the automatic "id" field that Roo creates? Try to let Roo handle it first automatically and implement your own when that works.

          Or is this what you already did?
          Last edited by MiB; Mar 23rd, 2011, 03:38 PM.

          Comment


          • #6
            Another point to make is that you should write your own tests for your entities, so that you know they are sound in design and are working with your database.

            Comment


            • #7
              I changed the nullable=false to @NotNull.

              That made the exception go away but it is not creating anything. I'm thinking that I should tell roo to make everything java and they debug that.

              Roo looked so easy but I'm having difficulty with only 10 tables.

              Comment


              • #8
                it is easy to get started, but it's important to work with the structure Roo gives you and deviate from it only when there's something to be gained and you do know what you're doing, which most spring developers will after some experience.

                Post your roo-script, so others (and you) can recreate your project. Currently easiest way is to copy and paste from the log.roo file into a new [projectName].roo file. I maintain that for all my roo projects in case I need to start over.

                What about junit tests such as unit tests and integration tests? Have you written them and are they running green?

                I do recommend the book "Spring Roo in Action" from Manning. It's currently in MEAP.

                Comment


                • #9
                  Thanks for the input. here is my log.roo
                  Code:
                  // Spring Roo 1.1.1.RELEASE [rev 156ccd6] log opened at 2011-03-15 16:09:16
                  project --topLevelPackage com.imthekingofthecastle --projectName KingOfTheCastle --java 6 
                  persistence setup --database HYPERSONIC_PERSISTENT --databaseName king-dev --provider HIBERNATE 
                  entity --class ~.domain.StuffCategory  
                  field string Name
                  field string Description
                  hint
                  entity --class Stuff 
                  field reference --type ~.domain.StuffCategory --fieldName stuffCategory
                  hint
                  field date --fieldName dateAvailable --type date
                  field date --fieldName dateRemoved --type date
                  field number --fieldName Speed --type java.lang.Float
                  field number --fieldName Strength --type java.lang.Float
                  field number --fieldName agility --type java.lang.Float
                  field number --fieldName shield --type java.lang.Float
                  field number --fieldName firePower --type java.lang.Float
                  entity --class TaskGroup 
                  field string name
                  field string description
                  entity --class MasterTasks
                  field string name --notNull 
                  field string description --notNull 
                  hint
                  field reference --type ~.domain.TaskGroup --fieldName taskGroup
                  field reference --type ~.domain.MasterTasks --fieldName parentTask 
                  field boolean morningTask
                  field boolean eveningTask
                  field number minimumTime --type java.lang.Float
                  field number idealTime --type java.lang.Float
                  field number maximumTime --type java.lang.Float
                  entity --class Owner --testAutomatically 
                  field string name --notNull 
                  field email template 
                  field string password 
                  field string passwordHint 
                  entity --class FancyName --testAutomatically --table FancyName
                  field string --fieldName fancyName --column FancyName 
                  field boolean --fieldName forBoy --column ForBoy 
                  field boolean --fieldName forGirl --column ForGirl 
                  entity --class Player
                  field boolean --fieldName isGirl 
                  field number --fieldName gameLevel --column GameLevel --type java.lang.Integer
                  field number --fieldName stage --column Stage --type java.lang.Integer
                  field string lastTaskCompleted --column LastTaskCompleted
                  entity --class PlayerStuff --table PlayerStuff 
                  entity --class PlayerMasterTask --testAutomatically --table PlayerMasterTask
                  field reference --fieldName player --type  ~.domain.Player 
                  field reference --fieldName task --type ~.domain.MasterTasks 
                  field reference --fieldName taskGroup --type ~.domain.TaskGroup 
                  field boolean --fieldName morningTask 
                  field boolean --fieldName eveningTask 
                  field number --fieldName minimumTime --column MinimumTime --type java.lang.Float
                  field number --fieldName idealTime --column IdealTime --type java.lang.Float
                  field number --fieldName maximumTime --column MaximumTime --type java.lang.Float
                  field date --fieldName whenSet --column WhenSet --type java.util.Calendar
                  entity --class PlayerDateTask --testAutomatically --table PlayerDateTask
                  field reference --fieldName playerMasterTask  --type ~.domain.PlayerMasterTask
                  field date --fieldName dateObserved --column DateObserved --notNull --type java.util.Calendar
                  field boolean --fieldName morningTask --column MorningTask 
                  field boolean --fieldName eveningTask --column EveningTask 
                  field number --fieldName observedTime --column ObservedTime --type java.lang.Float 
                  controller all --package ~.web
                  perform eclipse
                  security setup
                  version
                  controller all --package ~.web
                  addon remove --bundleSymbolicName com.xsoftwarelabs.spring.roo.addon.typicalsecurity
                  osgi start --url http://spring-roo-addon-typical-security.googlecode.com/files/com.xsoftwarelabs.spring.roo.addon.typicalsecurity-0.1.4.BUILD-SNAPSHOT.jar
                  entity --class ~.domain.User --testAutomatically --permitReservedWords
                  field string --fieldName firstName --sizeMin 1 --notNull
                  field string --fieldName lastName --sizeMin 1 --notNull
                  field string --fieldName emailAddress --sizeMin 1 --notNull --unique
                  field string --fieldName password --sizeMin 1 --notNull
                  field date --fieldName activationDate --type java.util.Date 
                  field string --fieldName activationKey 
                  field boolean --fieldName enabled 
                  field boolean --fieldName locked 
                  entity --class ~.domain.Role --testAutomatically --permitReservedWords
                  field string --fieldName roleName --sizeMin 1 --notNull --unique
                  field string --fieldName roleDescription --sizeMin --sizeMax 200 --notNull
                  entity --class ~.domain.UserRole --testAutomatically
                  field reference --fieldName userEntry --type ~.domain.User --notNull
                  field reference --fieldName roleEntry --type ~.domain.Role --notNull
                  finder add findUsersByEmailAddress --class ~.domain.User
                  finder add findUsersByActivationKeyAndEmailAddress --class ~.domain.User
                  finder add findUserRolesByUserEntry --class ~.domain.UserRole
                  controller scaffold --class ~.web.UserController --entity ~.domain.User
                  controller scaffold --class ~.web.RoleController --entity ~.domain.Role
                  controller scaffold --class ~.web.UserRoleController --entity ~.domain.UserRole
                  email sender setup --hostServer smtp.gmail.com --port 587 --protocol SMTP --username [email protected] --password password4me
                  typicalsecurity setup
                  osgi obr url add --url http://spring-roo-repository.springsource.org/repository.xml
                  osgi obr start --bundleSymbolicName org.springframework.roo.wrapping.hsqldb
                  osgi obr url remove --url http://spring-roo-repository.springsource.org/repository.xml
                  // Spring Roo 1.1.1.RELEASE [rev 156ccd6] log closed at 2011-03-23 15:00:20
                  I looked at the controller for Owner and it is empty:
                  Code:
                  import com.imthekingofthecastle.domain.Owner;
                  import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
                  import org.springframework.stereotype.Controller;
                  import org.springframework.web.bind.annotation.RequestMapping;
                  
                  @RooWebScaffold(path = "owners", formBackingObject = Owner.class)
                  @RequestMapping("/owners")
                  @Controller
                  public class OwnerController {
                  }
                  Is it supposed to be empty? With the guts in some other aj file?

                  Comment


                  • #10
                    Thanks again for helping.

                    Originally posted by MiB View Post
                    IMHO ...snip...
                    And is "ownerID" supposed to replace the automatic "id" field that Roo creates? Try to let Roo handle it first automatically and implement your own when that works.

                    Or is this what you already did?
                    The schema I'm trying to implement was created on a white board. There we designed the join tables to use the ID of the many-to-many etc.

                    That is why I have my own IDs.

                    I have never created a Hibernate project either (this is my first roo proj). I have read that Hibernate also creates its own id. So if I use roo & Hibernate I'll be getting 2 ids/Entity class?

                    I'm obviously very confused on how I'm supposed to do things "the roo way".

                    Comment


                    • #11
                      I used the JPA examples from this article to implement the relationship annotations.

                      Could that be my issue? @JPA annotations mixed with @Roo annotations with @Hibernate annotations?

                      Comment


                      • #12
                        The "Roo way" for entity relationships is really the JPA way.
                        There's no need to worry much about using your own id fields unless you have specific requirements for these.
                        You manage entity relations by using field set the field reference.

                        For many to many relationships there is a good example in part 8.3 of the roo documentation. It's a great idea to both read the documentation for Roo as well as the Spring framework.


                        I can also recommend again the book Spring Roo in Action MEAP.. Those $30 can save save you a lot of time.

                        Comment


                        • #13
                          Originally posted by KingAndrew View Post
                          I used the JPA examples from this article to implement the relationship annotations.

                          Could that be my issue? @JPA annotations mixed with @Roo annotations with @Hibernate annotations?
                          Not necessarily so, but it's good idea to proceed in a minimalistic way as things then only may not work in limited ways, which makes it easier to fix problems and add on functionality piece by piece. Also, Roo uses JPA2 and I think that article may be focused on JPA first version. It's still a good overview though.

                          As I've already pointed out it's only necessary to set names of tables and nullable on the database itself when you actually need it, becauseof requirements.
                          It's a good practice to do as little as needed first, making sure your minimal set of planned entities work by writing tests that do things with these they way you expect them to work.
                          Only when those tests run green then add the web tier for your entities. This way you'll be sure the problem is not within your domain objects themselves. Of course it's very possible to have web tier and still make use of those tests. My point is that if you have verified your domain objects by tests including validation constraints, then you'll know that those functions are operative, making it easier to design the view and find out what's not working the way you want. Tests save time in my experience.

                          While you may not need it for simpler applications a good book on JPA2 is a good idea IMHO. I got both "Pro JPA 2: Mastering the Java Persistence API" on apress and "Java Persistence with Hibernate" from Manning, though the latter is not JPA2.
                          Last edited by MiB; Mar 25th, 2011, 08:10 AM.

                          Comment

                          Working...
                          X