tags:

views:

164

answers:

3

Hi,

I'm trying to retrieve a list of fields from a table using Hibernate that have unique constraints on them.

Is this possible? How can I do this?

Thanks

+4  A: 

You can get this from the meta-information supplied by annotations:

  1. Get the java.lang.reflect.* member (either Field or Method) on which you put the annotations (entityClass.getDeclaredFields() for example)
  2. For each member, obtain the @Column annotation - Column c = field.getAnnotation(Column.class)
  3. Get the unique attribute. If it is true, add the underlying field to a list.
Bozho
fantastic. thanks
Michael Jones
+1 I think this also deserves an upvote
stacker
A: 

it doesn't work. here's my code to retrieve the 'unique' attiribute on method 'Column' annotations:

    ArrayList<String> fields = new ArrayList<String>();

    javax.persistence.Column column;
    // otherwise compute them, store in a local variable and return them
    for (Method method : this.getClass().getDeclaredMethods())
    {
        column = method.getAnnotation(javax.persistence.Column.class);

        if (column.unique() == true)
        {
            fields.add(column.name());
        }
    }

i get the following exception:

    Exception in thread "pool-11-thread-1" java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/persistence/GenerationType
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
    at java.lang.Class.getDeclaredMethods(Class.java:1791)
    at sun.reflect.annotation.AnnotationType$1.run(AnnotationType.java:86)
    at sun.reflect.annotation.AnnotationType$1.run(AnnotationType.java:83)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:82)
    at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:66)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:202)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52)
    at java.lang.reflect.Method.declaredAnnotations(Method.java:693)
    at java.lang.reflect.Method.getAnnotation(Method.java:679)
    at com.example.insto.plugin.orm.OrmObject.getUniqueFields(OrmObject.java:290)
Michael Jones
1. add this as an update to your original question. It is not an answer. 2. show your entity. 3. The exception means class incompatibilities.
Bozho
A: 

here's the entity - this is too big for the comments field

/**
 * Ip generated by hbm2java
 */
@Entity
@Table(name="ip"
,catalog="orwell"
, uniqueConstraints = @UniqueConstraint(columnNames="ip") 
)
public class Ip extends OrmObject implements java.io.Serializable {


 private Long id;
 private String ip;
 private Set<PageView> pageViewsForRemoteIp = new HashSet<PageView>(0);
 private Set<PageView> pageViewsForLocalIp = new HashSet<PageView>(0);

public Ip() {
}

public Ip(String ip, Set<PageView> pageViewsForRemoteIp, Set<PageView> pageViewsForLocalIp) {
   this.ip = ip;
   this.pageViewsForRemoteIp = pageViewsForRemoteIp;
   this.pageViewsForLocalIp = pageViewsForLocalIp;
}

 @Id @GeneratedValue(strategy=IDENTITY)

@Column(name="id", unique=true, nullable=false)
public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}

@Column(name="ip", unique=true, length=16)
public String getIp() {
    return this.ip;
}

public void setIp(String ip) {
    this.ip = ip;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="ipByRemoteIp")
public Set<PageView> getPageViewsForRemoteIp() {
    return this.pageViewsForRemoteIp;
}

public void setPageViewsForRemoteIp(Set<PageView> pageViewsForRemoteIp) {
    this.pageViewsForRemoteIp = pageViewsForRemoteIp;
}
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="ipByLocalIp")
public Set<PageView> getPageViewsForLocalIp() {
    return this.pageViewsForLocalIp;
}

public void setPageViewsForLocalIp(Set<PageView> pageViewsForLocalIp) {
    this.pageViewsForLocalIp = pageViewsForLocalIp;
}
Michael Jones
You should edit the question and post it on the body of it.
Kamia