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
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
You can get this from the meta-information supplied by annotations:
java.lang.reflect.*
member (either Field
or Method
) on which you put the annotations (entityClass.getDeclaredFields()
for example)@Column
annotation - Column c = field.getAnnotation(Column.class)
unique
attribute. If it is true, add the underlying field to a list.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)
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;
}