ファイルをデータベース (mysql) にアップロードできます。同じファイルを再度アップロードしようとすると、次のようになりました。
"... 内部例外: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: キー 'filename' の重複エントリ 'gg.txt' エラー コード: 1062 呼び出し: INSERT INTO RESOURCE (filename, override, product_id) VALUES (? , ?, ?) bind => [gg.txt, false, 1] ..."
フラグがtrueに設定されている場合、アップロードされたファイルのオーバーライドを有効にしたいと思います。
Upload.StartedListener、Upload.ProgressListener、Upload.Receiver、Upload.FinishedListener、Upload.SucceededListener、および Upload.FailedListener を実装するクラスを作成しました。
ここにコードの断片があります
@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;
}
アップロードされたファイルのオーバーライドを有効にする方法についてアドバイスをいただけますか?
resourceDao の save メソッド:
@Override
public void save(Resource entity) {
em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();
}
em - エンティティマネージャー
リソース テーブル
@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();
}
}