-->
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.  [ 2 posts ] 
Author Message
 Post subject: place for annotations instructions in interface or class
PostPosted: Fri Jan 25, 2008 10:20 pm 
Beginner
Beginner

Joined: Mon Jan 07, 2008 4:13 am
Posts: 22
hi!,

i`m a beginner in hibernate, i have question about annotations informations place, if i use interface and one inherited class.

Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="value_type",
   discriminatorType=DiscriminatorType.STRING
)
@Table(name="house_property_value")
@javax.persistence.SequenceGenerator(
   name="seq_house_property_value",
   sequenceName="s_house_property_value",
   allocationSize=1
)
public interface HousePropertyValue extends Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seq_house_property_value")
   public Long getId();
   public void setId(Long id);
   @Column(name="value_key", nullable = false)
   public String getValueKey();
   public void setValueKey(String valueKey);
   @ManyToOne (fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   @JoinColumn(name = "fk_house_id", referencedColumnName = "id", nullable = false)
   public House getHouse();
   public void setHouse(House house);
   public Object getValue();
   public void setValue(Object value);
}


if i have this interface, it is neccesary to have all annotations instructions here, or it is possible to remove these instruction to inherited class ? Or is there any rules for position of these instructions ?

inherited clss:

Code:
@Entity
@DiscriminatorValue("DATE")
@javax.persistence.SequenceGenerator(
   name="seq_house_property_value",
   sequenceName="s_house_property_value",
   allocationSize=1
)
public class HousePropertyDateValue implements HousePropertyValue {

   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seq_house_property_value")
   private Long id;
   private House house;
   private String valueKey;
   @Column(name="value_date", nullable=false)
   private Date value = new Date();
   public HousePropertyDateValue() {}
   public HousePropertyDateValue(Date value) {    this.value = value;    }
   public Long getId() {       return id;    }
   public void setId(Long id) {         this.id = id;    }
   public String getValueKey() {         return valueKey;    }
   public void setValueKey(String valueKey) {   this.valueKey = valueKey;    }
   public House getHouse() {        return house;    }
   public void setHouse(House house) {        this.house = house;    }
   public Date getValue()    {        return value;    }
   public void setValue( Date value )    {        this.value = value;    }
   public void setValue( Object value )    {        this.value = (Date) value;    }
}


thanks!

Ivan


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 3:33 pm 
Regular
Regular

Joined: Mon Aug 20, 2007 6:47 am
Posts: 74
Location: UK
Hi Ivan,

May I suggest using an abstract class rather than an interface?

for example

Code:

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="value_type",
   discriminatorType=DiscriminatorType.STRING
)
@Table(name="house_property_value")
@javax.persistence.SequenceGenerator(
   name="seq_house_property_value",
   sequenceName="s_house_property_value",
   allocationSize=1
)
public abstract class HousePropertyValue implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seq_house_property_value")
   Long id;

   @Column(name="value_key", nullable = false)
   String valueKey;

   @ManyToOne (fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   @JoinColumn(name = "fk_house_id", referencedColumnName = "id", nullable = false)
   House house;

   
   public abstract Object getValue();
   public abstract void setValue(Object value);
}



Notice that I've annotated properties rather than methods (just my personal preference) and ommitted the getters and setters. This now means you can forget all about id,valueKey and house in subclasses!

Your subclass would then look something like this:

Code:
@Entity
@DiscriminatorValue("DATE")
@javax.persistence.SequenceGenerator(
   name="seq_house_property_value",
   sequenceName="s_house_property_value",
   allocationSize=1
)
public class HousePropertyDateValue extends HousePropertyValue {

   @Column(name="value_date", nullable=false)
   private Date value = new Date();

   public HousePropertyDateValue() {}
   public HousePropertyDateValue(Date value) {    this.value = value;    }

   public Object getValue()    {        return value;    }
   public void setValue( Date value )    {        this.value = value;    }
   public void setValue( Object value )    {        this.value = (Date) value;    }
}


One last thing.. depending on the version of Java you are using - why not use generics with getValue and setValue?

Code:

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="value_type",
   discriminatorType=DiscriminatorType.STRING
)
@Table(name="house_property_value")
@javax.persistence.SequenceGenerator(
   name="seq_house_property_value",
   sequenceName="s_house_property_value",
   allocationSize=1
)
public abstract class HousePropertyValue<T> implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seq_house_property_value")
   Long id;

   @Column(name="value_key", nullable = false)
   String valueKey;

   @ManyToOne (fetch = FetchType.LAZY, cascade = CascadeType.ALL)
   @JoinColumn(name = "fk_house_id", referencedColumnName = "id", nullable = false)
   House house;

   
   public abstract T getValue();
   public abstract void setValue(T value);
}



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

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.