Asif
Asif

Reputation: 1302

Foreign key not created with Hibernate Spring integrated code

I am using org.springframework.orm.hibernate4.LocalSessionFactoryBean.LocalSessionFactoryBean and have the following classes:

@Entity
@Table(name="ORDERS")
public class Order implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private long id;
    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn
    private Item item;
    ...
    }

and

@Entity
@Table(name="ITEM")
public class Item implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private long id;
private String img;
private long price;
@OneToOne(fetch=FetchType.LAZY)
@JoinColumn
private ItemCategory category;
private String description;
...
}

But when the server comes up, for the ORDERS table there is no Foreign Key created:

 mysql> desc ORDERS;
    +------------+--------------+------+-----+---------+----------------+
    | Field      | Type         | Null | Key | Default | Extra          |
    +------------+--------------+------+-----+---------+----------------+
    | id         | bigint(20)   | NO   | PRI | NULL    | auto_increment |
    | item       | tinyblob     | YES  |     | NULL    |                |
    | placedDate | datetime     | YES  |     | NULL    |                |
    | quantity   | int(11)      | NO   |     | NULL    |                |
    | status     | varchar(255) | YES  |     | NULL    |                |
    +------------+--------------+------+-----+---------+----------------+
    5 rows in set (0.01 sec)

What could be wrong? Please suggest.

Thanks

Upvotes: 0

Views: 196

Answers (1)

Asif
Asif

Reputation: 1302

This got resolved by moving the @OneToOne over the getter method.

There should be punishment for impatience but can anyone tell me why it did not work when the annotation was on the attribute itself?

Thanks

Upvotes: 1

Related Questions