Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9116095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:35:21+00:00 2026-06-17T04:35:21+00:00

I can upload file into database (mysql). When i try to upload same file

  • 0

I can upload file into database (mysql).
When i try to upload same file again i got:

“…
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry ‘gg.txt’ for key ‘filename’
Error Code: 1062
Call: INSERT INTO RESOURCE (filename, override, product_id) VALUES (?, ?, ?)
bind => [gg.txt, false, 1]
…”

Now I want to enable override uploaded file when i have flag set on true.

I have class written which implements Upload.StartedListener, Upload.ProgressListener, Upload.Receiver, Upload.FinishedListener, Upload.SucceededListener and Upload.FailedListener

here’s fragment of the code

@Override
public void uploadStarted(StartedEvent event) {
    progress.setValue(0f);
    progress.setVisible(true);
    cancelButton.setVisible(true);
    uploadField.setVisible(false);
}

@Override
public void updateProgress(long readBytes, long contentLength) {
    progress.setValue(new Float(readBytes / (float)contentLength));
}

@Override
public OutputStream receiveUpload(String filename, String mimeType) {
    OutputStream outputStream = null;
    try {
        String dir = resourceDao.getAbsoluteDir(product);
        // ensures that the dir exists
        new File(dir).mkdirs();
        uploadedFile = new File(dir + filename);
        if (!uploadedFile.exists()) { 
            uploadedFile.createNewFile();
        }
        outputStream = new FileOutputStream(uploadedFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return outputStream;
}

@Override
public void uploadSucceeded(SucceededEvent event) {
    checkArgument(product != null, "Product is null."); 
    String filename = uploadedFile.getName();
    Resource res = new Resource();
    res.setFilename(filename);
    res.setProduct(product);
    product.getResources().add(res);
    productDao.save(product);
    eventBus.post(new ResourceAddedEvent(res));
    uploadedFile = null;
}

@Override
public void uploadFailed(FailedEvent event) {
    if (uploadedFile != null && uploadedFile.exists()) {
        uploadedFile.delete();
    }
    uploadedFile = null;        
}

Can you give me some advice, how to enable override of uploaded files?

save method of resourceDao:

@Override
public void save(Resource entity) {
    em.getTransaction().begin();
    em.persist(entity);
    em.getTransaction().commit();
}

em – EntityManager

Resource Table

@Entity(name = Resource.ENTITY_NAME)
public class Resource {

public static final String ENTITY_NAME = "resource"; 

public static final String COLUMN_ID = "id"; 

public static final String COLUMN_PRODUCT_ID = "product_id"; 

public static final String COLUMN_FILENAME = "filename"; 

public static final String COLUMN_OVERRIDE = "override"; 

public static final String ATTRIBUTE_ID = COLUMN_ID;

public static final String ATTRIBUTE_FILENAME = COLUMN_FILENAME;

public static final String ATTRIBUTE_PRODUCT = "product"; 

public static final String ATTRIBUTE_OVERRIDE = "override"; 


// User identifier. 
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = COLUMN_ID)
private int id;


//Product that the resource belongs to.
@ManyToOne
@JoinColumn(name = COLUMN_PRODUCT_ID, nullable = false)
private Product product;

//Path for the file.
@Column(name = COLUMN_FILENAME, nullable = false, unique = true)
private String filename;

//Flag that indicates if the resource can be override.
@Column(name = COLUMN_OVERRIDE, nullable = false)
private boolean isOverride;

public static String getLabel(String attribute) {
    return Messages.getLabel("product_resource." + attribute); //$NON-NLS-1$
}

//...getters and setters...

@Override
public int hashCode() {
    return Objects.hash(id, filename, getProduct());
}

@Override
public boolean equals(Object obj) {
    if (obj == this) {
        return true;
    }
    if (obj instanceof Resource) {
        Resource resource = (Resource)obj;
        if (resource.id != this.id) {
            return false;
        }
        // if the id match - check the following attributes
        return Objects.equals(filename, resource.filename) && //
                Objects.equals(getProduct(), resource.getProduct());
    }
    return false;
}

@Override
public String toString() {
    return com.google.common.base.Objects.toStringHelper(this) //
            .add(ATTRIBUTE_ID, id) //
            .add(ATTRIBUTE_FILENAME, filename) //
            .toString();
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:35:22+00:00Added an answer on June 17, 2026 at 4:35 am

    You want to use

    em.merge(entity);
    

    instead of

    em.persist(entity);
    

    persist() directly stores and entity. A merge() will try to update the current entity. If it doesn’t exist it will store it.

    If this is not working you should show us your entity code. IIRC correctly the merging is done on the @Id column.

    Update

    You are always creating a new entity and save that one. For the merging to work you should do a lookup in your existing data first, if it already exists.

    Create a @NamedQuery on your entity (its has to be wrapped in a @NamedQueries). Create a find existing file method on your dao. Update that entity and afterwards merge for saving.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to import data from a CSV file into MySQL database using JSP/Servlet.
I have a page where a user can upload a file along with some
How can I upload a File (graphic, audio and video file) with Android using
I have a form where the user can upload a zip file, it works
I have string like this: G:\Projects\TestApp\TestWeb\Files\Upload\file.jpg How can I remove all text before Files
How can I upload some text info (a text string) and an image file
Can I use FlexUnit to unit test a File Upload and delete? Are those
How can one best test a controller action which receives a file upload using
I am using the jQuery-File-Upload widget (although I believe this question can generalize to
I have a page where the user can dynamically add file upload boxes. Adding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.