I have to create a table whose DDL looks like this:
CREATE TABLE `feeds` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(255) default NULL,
`url` varchar(255) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_feeds_on_url` (`url`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
One thing that stands out is that there is a unique key called 'index_feeds_on_url', it uses the 'url' column.
I am trying to generate the appropriate table definition using Hibernate Annotations. The current version uses the Feed.java class as defined below, but the DDL that i get is:
CREATE TABLE `feeds` (
`id` int(11) NOT NULL auto_increment,
`title` varchar(255) default NULL,
`url` varchar(255) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`url`),
KEY `index_feeds_on_url` (`url`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
This is creating two keys - one called 'index_feeds_on_url' which is non-unique, and another one called 'url' which is unique. Basically, I cannot find a way in documentation to apply a unique constraint on an index as opposed to a column, especially when an index could be based on a number of columns and be named differently than any of the columns.
Hibernate version:
3.2.4
Mapping documents:
package net.feedscape.server.model;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Index;
@Entity
@Table(name="feeds")
public class Feed implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(unique=true, name="url")
@Index(name="index_feeds_on_url")
private String url;
@Column(name="title")
private String title;
@OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name="feed_id")
private Set<Article> articles = new HashSet<Article>();
public Integer getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Set<Article> getArticles() {
return articles;
}
public void setArticles(Set<Article> articles) {
this.articles = articles;
}
public Feed(String url, String title) {
super();
this.url = url;
this.title = title;
}
public Feed() {
super();
}
}
|