Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Why is roo/hibernate inserting null?

  1. #1

    Default 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. #2

    Default

    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 ?

  3. #3

    Question

    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.

  4. #4

    Default

    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 ?

  5. #5
    Join Date
    Aug 2010
    Location
    Goteborg, Sweden
    Posts
    434

    Default

    Quote 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 at 02:38 PM.

  6. #6
    Join Date
    Aug 2010
    Location
    Goteborg, Sweden
    Posts
    434

    Lightbulb

    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.

  7. #7

    Default

    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.

  8. #8
    Join Date
    Aug 2010
    Location
    Goteborg, Sweden
    Posts
    434

    Default

    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.

  9. #9

    Default

    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 rohitsghatoltest@gmail.com --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?

  10. #10

    Default

    Thanks again for helping.

    Quote 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".

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •