Search code examples
hibernategrailsgrails-orm

Hibernate cannot find table in Database First development


I'm running into another issue with my Grails application. I created and populated my Site_Menu table as below. When I try to access the controller functions, I get an error that the table cannot be found. I'll provide my code below.

CREATE TABLE `Site_Menu` (
  `ID` bigint(20) NOT NULL AUTO_INCREMENT,
  `MenuDisplay` varchar(100) NOT NULL,
  `MenuLink` varchar(256) NOT NULL,
  `MenuTarget` varchar(25) DEFAULT NULL,
  `MenuSortOrder` int(11) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

INSERT INTO Site_Menu (MenuDisplay, MenuLink, MenuTarget, MenuSortOrder)
VALUES ('Home Page', 'index.html', null, 1);
INSERT INTO Site_Menu (MenuDisplay, MenuLink, MenuTarget, MenuSortOrder)
VALUES ('About the BPO Elks', 'aboutelks.html', null, 2);
INSERT INTO Site_Menu (MenuDisplay, MenuLink, MenuTarget, MenuSortOrder)
VALUES ('Drug Awareness Program', 'dap.html', null, 3);
INSERT INTO Site_Menu (MenuDisplay, MenuLink, MenuTarget, MenuSortOrder)
VALUES ('Elks National Fund', 'enf.html', null, 4);
INSERT INTO Site_Menu (MenuDisplay, MenuLink, MenuTarget, MenuSortOrder)
VALUES ('G.U.M.B.O. Games', 'majorproject.html', null, 5);
INSERT INTO Site_Menu (MenuDisplay, MenuLink, MenuTarget, MenuSortOrder)
VALUES ('Louisiana Elks Fund', 'lef.html', null, 6);
INSERT INTO Site_Menu (MenuDisplay, MenuLink, MenuTarget, MenuSortOrder)
VALUES ('Scholarships', 'scholarships.html', null, 7);
INSERT INTO Site_Menu (MenuDisplay, MenuLink, MenuTarget, MenuSortOrder)
VALUES ('Veterans Services', 'vavs.html', null, 8);
INSERT INTO Site_Menu (MenuDisplay, MenuLink, MenuTarget, MenuSortOrder)
VALUES ('About Us', 'aboutus.html', null, 9);
INSERT INTO Site_Menu (MenuDisplay, MenuLink, MenuTarget, MenuSortOrder)
VALUES ('Our Facilities', 'facilities.html', null, 10);
INSERT INTO Site_Menu (MenuDisplay, MenuLink, MenuTarget, MenuSortOrder)
VALUES ('Lodge Officers & Committee Chairs', 'officers.html', null, 11);
INSERT INTO Site_Menu (MenuDisplay, MenuLink, MenuTarget, MenuSortOrder)
VALUES ('Contact Us', 'contactus.html', null, 12);

COMMIT;

I have my domain model like:

package plaquemineelks

class SiteMenu {

    Long id

    String title
    String link
    String target
    Integer sortOrder

    static constraints = {
        title (blank: false)
        link (blank: false)
        sortOrder (blank: false)
    }

    static mapping = {
        table: "Site_Menu"
        id column: "ID"
        title column: "MenuDisplay"
        link column: "MenuLink"
        target column: "MenuTarget"
        sortOrder column: "MenuSortOrder"
        version false
    }
}

The controller looks like:

package plaquemineelks

class SiteMenuController {

    def index() { }

    def getMenu() {
        var results = SiteMenu.findAll()

        render(contentType: 'text/json') {[
            'results': results,
            'status': results ? "OK" : "Nothing present"
        ]}
    }
}

The error I get reads:

URI /PlaquemineElks/SiteMenu/getMenu Class com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException Message Table 'PlaquemineElks.site_menu' doesn't exist

I have tried renaming the table and class, I recreated everything. My Calendar class and controller works fine, why is this one giving me problems?

Thanks for the assistance on this issue and my last.


Solution

  • Basing on official docs, instead of:

    static mapping = {
        table: "Site_Menu"
        ...
    }
    

    use:

    static mapping = {
        table name: "Site_Menu"
        ...
    }
    

    or

    static mapping = {
        table "Site_Menu"
        ...
    }