-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How to create a unique constraint on a key/index
PostPosted: Thu Nov 01, 2007 5:35 pm 
Newbie

Joined: Wed Apr 06, 2005 12:30 am
Posts: 7
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();
}
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.