Babak Behzadi
Babak Behzadi

Reputation: 1256

Hibernate doesn't create an table

I'm implementing an Spring+Hibernate web application. In this application I have three entities as below:

User:

@Entity
@Table(name = "tbl_user")
public class User {

    @Id
    private long userId;

    @Column(nullable = false)
    private String username;
    @Column(nullable = false)
    private String password;
    private Calendar joinDate;
    private String mobile;
    private String displayName;
    private boolean active;
    private boolean searchable;
    @ManyToMany(mappedBy = "joinedUsers")
    private Collection<Session> joinedSessions;
    @OneToMany(mappedBy = "sender")
    private Collection<Message> messages;

    ////getters and setters
}

Session:

@Entity
@Table(name = "tbl_session")
public class Session {

    @Id
    private long sessionId;
    private Calendar startDate;
    @ManyToMany
    @JoinTable(
            name = "tbl_user_session",
            joinColumns = {@JoinColumn(name = "sessionId")},
            inverseJoinColumns = {@JoinColumn(name = "userId")}
    )
    private Collection<User> joinedUsers;
    @OneToMany(mappedBy = "session")
    private Collection<Message> messages;

        ////getters and setters
}

Message:

@Entity
@Table(name = "tbl_message")
public class Message {

    @Id
    private long messageId;

    @ManyToOne
    private User sender;
    @ManyToOne
    private Session session;
    private Calendar sendDate;
    private String content;
    private boolean mediaMessage;
    private boolean sent;
    private boolean read;        
   ////getters and setters
}

I've set hibernate.dialect as org.hibernate.dialect.MySQL5InnoDBDialect and hibernate.hbm2ddl.auto as create.

When I run tomcat JPA session will be created successfully and tables tbl_user, tbl_session and tbl_user_session will be created but hibernate does not create tbl_message.

Server log:

Hibernate: 
    alter table tbl_message 
        drop 
        foreign key FK_4unouf9cqiw2e35a7mae9latk
Hibernate: 
    alter table tbl_message 
        drop 
        foreign key FK_h06m8p8o7furulj37xh4fd7s7
Hibernate: 
    alter table tbl_user_session 
        drop 
        foreign key FK_o9ow3kvh6odmn7raj1r10ninx
Hibernate: 
    alter table tbl_user_session 
        drop 
        foreign key FK_j33qeb6km5ffswqfcms9c3xqj
Hibernate: 
    drop table if exists tbl_message
Hibernate: 
    drop table if exists tbl_session
Hibernate: 
    drop table if exists tbl_user
Hibernate: 
    drop table if exists tbl_user_session
Hibernate: 
    create table tbl_message (
        messageId bigint not null,
        content varchar(255),
        mediaMessage bit not null,
        read bit not null,
        sendDate datetime,
        sent bit not null,
        sender_userId bigint,
        session_sessionId bigint,
        primary key (messageId)
    ) ENGINE=InnoDB
Hibernate: 
    create table tbl_session (
        sessionId bigint not null,
        startDate datetime,
        primary key (sessionId)
    ) ENGINE=InnoDB
Hibernate: 
    create table tbl_user (
        userId bigint not null,
        active bit not null,
        displayName varchar(255),
        joinDate datetime,
        mobile varchar(255),
        password varchar(255) not null,
        searchable bit not null,
        username varchar(255) not null,
        primary key (userId)
    ) ENGINE=InnoDB
Hibernate: 
    create table tbl_user_session (
        sessionId bigint not null,
        userId bigint not null
    ) ENGINE=InnoDB
Hibernate: 
    alter table tbl_message 
        add constraint FK_4unouf9cqiw2e35a7mae9latk 
        foreign key (sender_userId) 
        references tbl_user (userId)
Hibernate: 
    alter table tbl_message 
        add constraint FK_h06m8p8o7furulj37xh4fd7s7 
        foreign key (session_sessionId) 
        references tbl_session (sessionId)
Hibernate: 
    alter table tbl_user_session 
        add constraint FK_o9ow3kvh6odmn7raj1r10ninx 
        foreign key (userId) 
        references tbl_user (userId)
Hibernate: 
    alter table tbl_user_session 
        add constraint FK_j33qeb6km5ffswqfcms9c3xqj 
        foreign key (sessionId) 
        references tbl_session (sessionId)

Any help is appreciated in advance.

Upvotes: 2

Views: 1000

Answers (1)

Veselin Davidov
Veselin Davidov

Reputation: 7071

create table tbl_message (
    messageId bigint not null,
    content varchar(255),
    mediaMessage bit not null,
    read bit not null,
    sendDate datetime,
    sent bit not null,
    sender_userId bigint,
    session_sessionId bigint,
    primary key (messageId)
) ENGINE=InnoDB

This sql fails because Read is reserved rule and should become:

create table tbl_message (
    messageId bigint not null,
    content varchar(255),
    mediaMessage bit not null,
    `read` bit not null,
    sendDate datetime,
    sent bit not null,
    sender_userId bigint,
    session_sessionId bigint,
    primary key (messageId)
) ENGINE=InnoDB

Create the table by yourself or specify another name for that column with annotation.

Upvotes: 2

Related Questions