Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call another method in the pojo class setter method [session file uploading application] of Hibernate and struts 2
    text
    copied!<p>Hi Friends i am developing an web application which uses hibernate and struts2. i am creating photo album i successfully done that with out using hibernate but, with hibernate it is having problem while inserting to the database. the module works like this as soon as the file uploaded it will insert the FileName, Contenttype,id and it suppose to insert the Image File(byte[]) Content also but it is showing Null in table value.. my code goes like this...</p> <pre><code>@Entity @Table(name="PHOTOALBUM") public class User implements Serializable { @Id @GeneratedValue @Column(name="PHOTO_ID") private Long id; @Column(name="IMAGE") private byte[] Image; @Column(name="CONTENT_TYPE") private String userImageContentType; @Column(name="PHOTO_NAME") private String userImageFileName; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUserImageFileName() { return userImageFileName; } public void setUserImageFileName(String userImageFileName) { this.userImageFileName = userImageFileName; } public String getUserImageContentType() { return userImageContentType; } public void setUserImageContentType(String userImageContentType) { this.userImageContentType = userImageContentType; } public byte[] getImage() { return Image; } public void setImage(byte[] Image) { Image=Change(this.getUserImage()); this.Image = Image; } @Transient private File userImage; public File getUserImage() { return userImage; } public void setUserImage(File userImage) { this.userImage = userImage; } public byte[] Change(File userImage) { // userImage=this.getUserImage(); // String name=userImage.getName(); // long len=userImage.length(); byte[] bFile = new byte[(int) userImage.length()]; try { FileInputStream fileInputStream = new FileInputStream(userImage); fileInputStream.read(bFile); fileInputStream.close(); } catch(Exception e) { e.printStackTrace(); } // System.out.println("The Name Of File In Pojo Class Is:="+ name); //System.out.println("The Length Of File In Pojo Class Is:="+ len); //System.out.println("The Content Of File In Pojo Class Is:="+ bFile); return bFile; } } </code></pre> <p>and i am saving the values like this</p> <pre><code> public class UserDAOImpl implements UserDAO { @SessionTarget Session session; @TransactionTarget Transaction transaction; /** * Used to save or update a user. */ @Override public void saveOrUpdateUser(User user) { try { session.saveOrUpdate(user); } catch (Exception e) { transaction.rollback(); e.printStackTrace(); } } /** * Used to delete a user. */ @Override public void deleteUser(Long userId) { try { User user = (User) session.get(User.class, userId); session.delete(user); } catch (Exception e) { transaction.rollback(); e.printStackTrace(); } } /** * Used to list all the users. */ @SuppressWarnings("unchecked") @Override public List&lt;User&gt; listUser() { List&lt;User&gt; courses = null; try { courses = session.createQuery("from User").list(); } catch (Exception e) { e.printStackTrace(); } return courses; } /** * Used to list a single user by Id. */ @Override public User listUserById(Long userId) { User user = null; try { user = (User) session.get(User.class, userId); } catch (Exception e) { e.printStackTrace(); } return user; } } </code></pre> <p>the struts action mapping goes like this...</p> <pre><code> \&lt;package name="default" extends="hibernate-default"&gt; &lt;action name="saveOrUpdateUser"method="saveOrUpdate"class="com.srikanth.web.UserAction"&gt; &lt;result name="success" type="redirect"&gt;listUser&lt;/result&gt; &lt;/action&gt; &lt;action name="listUser" method="list" class="com.srikanth.web.UserAction"&gt; &lt;result name="success"&gt;/register.jsp&lt;/result&gt; &lt;/action&gt; &lt;action name="editUser" method="edit" class="com.srikanth.web.UserAction"&gt; &lt;result name="success"&gt;/register.jsp&lt;/result&gt; &lt;/action&gt; &lt;action name="deleteUser" method="delete" class="com.srikanth.web.UserAction"&gt; &lt;result name="success" type="redirect"&gt;listUser&lt;/result&gt; &lt;/action&gt; &lt;/package&gt; </code></pre> <p>and my jsp goes like this </p> <pre><code> \&lt;s:form action="saveOrUpdateUser"&gt; &lt;s:push value="user"&gt; &lt;s:hidden name="id" /&gt; &lt;s:file name="userImage" label="User Image" /&gt; &lt;s:submit /&gt; &lt;/s:push&gt; &lt;/s:form&gt; &lt;s:iterator value="userList" status="userStatus"&gt; &lt;s:property value="id" /&gt; &lt;s:property value="userImageFileName" /&gt; &lt;s:property value="userImageContentType" /&gt; &lt;img src='&lt;s:property value="userImage" /&gt;' alt"" /&gt; &lt;s:url id="deleteURL" action="deleteUser"&gt; &lt;s:param name="id" value="%{id}"&gt;&lt;/s:param&gt; &lt;/s:url&gt; &lt;s:a href="%{deleteURL}"&gt;Delete&lt;/s:a&gt; &lt;/s:iterator&gt; </code></pre> <p>I am trying to call the Change Method so that it convert the file to byte[] and stored it in byte[] Image variable using setter but it is not working.... So please help me with this one ..... thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload