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();
}
}
You want to use
instead of
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.