2

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.

4
  • en toch ... volgens uw logs ... 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 Commented Apr 30, 2015 at 11:59
  • It does according to your logs... Instead of create you might want to try update to preserve your data. Commented Apr 30, 2015 at 12:03
  • From your logs - create table tbl_message - this shows that hibernate is creating the table. Then why are you saying hibernate does not create tbl_message.? Commented Apr 30, 2015 at 12:12
  • 1
    Maybe the problem is Read is reserved word in mysql and the create statement that hibernate uses doesn't treat it carefully and doesn't quote it. Commented Apr 30, 2015 at 12:18

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

1 Comment

thanks I've changed the field name and it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.