Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

BLOBS and EJB3

Thread view: 
Java and Swing - 24 May 2006 16:34 GMT
Anyone know how I would store a BLOB using EJB3? for example if i want
to store a file.

thanks
Danno - 24 May 2006 16:55 GMT
My gift to you.

package com.mycompany.data;

import java.util.Date;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

@Entity(name="Resource")
@Table(name="ag_resource")
public class Resource implements  Serializable {

   private Long id;

   private String notes;

   private byte[] byteArray;

   private Date createdDate;

   private String description;

   private Date updatedDate;

   private String name;

   private String mimeType;

   public Resource() {
       setCreatedDate(new Date());
       setUpdatedDate(new Date());
   }

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

   @Id @GeneratedValue(strategy=GenerationType.AUTO)
   public Long getId() {
       return id;
   }

   public void setNotes(final String notes) {
       this.notes = notes;
   }

   @Lob @Basic(fetch = FetchType.EAGER)
   public String getNotes() {
       return notes;
   }

   public void setByteArray(final byte[] byteArray) {
       this.byteArray = byteArray;
   }

   @Lob @Basic(fetch = FetchType.EAGER)
   public byte[] getByteArray() {
       return byteArray;
   }

   public void setCreatedDate(final Date createdDate) {
       this.createdDate = createdDate;
   }

   public Date getCreatedDate() {
       return createdDate;
   }

   public void setDescription(final String description) {
       this.description = description;
   }

   @Lob @Basic(fetch = FetchType.EAGER)
   public String getDescription() {
       return description;
   }

   public void setUpdatedDate(final Date updatedDate) {
       this.updatedDate = updatedDate;
   }

   public Date getUpdatedDate() {
       return updatedDate;
   }

   public void setName(final String name) {
       this.name = name;
   }

   public String getName() {
       return name;
   }

   public void setMimeType(final String mimeType) {
       this.mimeType = mimeType;
   }

   public String getMimeType() {
       return mimeType;
   }

   public String toString() {
       return new ToStringBuilder(this)
       .append("name", getName())
       .append("mimeType", getMimeType())
       .append("notes", getNotes())
       .append("createdDate", getCreatedDate())
       .append("description", getDescription())
       .append("updatedDate", getUpdatedDate())
       .toString();
   }

   public boolean equals(Object object) {
       if (!(object instanceof Resource)) return false;
       Resource resource = (Resource) object;
       return new EqualsBuilder()
       .append(this.getName(), resource.getName())
       .append(this.getMimeType(), resource.getMimeType())
       .append(this.getNotes(), resource.getNotes())
       .append(this.getCreatedDate(), resource.getCreatedDate())
       .append(this.getDescription(), resource.getDescription())
       .append(this.getUpdatedDate(), resource.getUpdatedDate())
       .isEquals();
   }

   public int hashCode() {
       return new HashCodeBuilder()
       .append(getName())
       .append(getMimeType())
       .append(getNotes())
       .append(getCreatedDate())
       .append(getDescription())
       .append(getUpdatedDate())
       .toHashCode();
   }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.