25 Mayıs 2018 Cuma

Lombok @Data Anotasyonu

Giriş
Tüm private alanlar için getter/setter üretilmesini sağlar.

Örnek
Şöyle yaparız.
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Contrat implements Serializable{

  @Id @GeneratedValue
  private Long id;
  private Date dateDebut ;
  private Date dateFin ;
  @ManyToOne
  @JoinColumn(name = "Id_Project")
  private Project project;

  @ManyToOne
  @JoinColumn(name = "Id_AppUser")
  private AppUser appUser;
}

JPA @Basic Anotasyonu

Giriş
Açıklaması şöyle.
A basic attribute is one where the attribute class is a simple type such as String, Number, Date or a primitive. A basic attribute's value can map directly to the column value in the database.
The types and conversions supported depend on the JPA implementation and database platform. Any basic attribute using a type that does not map directly to a database type can be serialized to a binary database type.
The easiest way to map a basic attribute in JPA is to do nothing. Any attributes that have no other annotations and do not reference other entities will be automatically mapped as basic, and even serialized if not a basic type. The column name for the attribute will be defaulted, named the same as the attribute name, as uppercase.
fetch Alanı
Örnek ver

optional Alanı
Açıklaması şöyle. false ise alanın null değer olarak kaydedilmesini engeller.
@Basic(optional) is (should be) checked on runtime by persistence provider before saving to DB. @Column is a column definition in database and is used for schema generation :
Şöyle yaparız.
@Basic(optional = false)
@Column(name = "HABILITADO")
private short habilitado;

Servlet Part Arayüzü

Giriş
Şu satırı dahil ederiz.
import javax.servlet.http.Part
multipart/form-data POST isteğindeki bir part'ı temsil eder.

Bu istek şuna benzer
POST / HTTP/1.1
HOST: host.example.com
Cookie: some_cookies...
Connection: Keep-Alive
Content-Type: multipart/form-data; boundary=12345

--12345
Content-Disposition: form-data; name="sometext"

some text that you wrote in your html form ...
--12345
Content-Disposition: form-data; name="name_of_post_request" filename="filename.xyz"

content of filename.xyz that you upload in your form with input[type=file]
--12345
Content-Disposition: form-data; name="image" filename="picture_of_sunset.jpg"

content of picture_of_sunset.jpg ...
--12345--

getHeader metodu
Şöyle yaparız.
private String extractFileName(Part part) {

  String contentDisp = part.getHeader("content-disposition");
  String[] items = contentDisp.split(";");
  for (String s : items) {
    if (s.trim().startsWith("filename")) {
      return s.substring(s.indexOf("=") + 2, s.length() - 1);
    }
  }
  return "";
}
getInputStream metodu
Şöyle yaparız.
Part part = ...
InputStream stream = part.getInputStream();
Örnek
Şöyle yaparız.
InputStream input = part.getFile().getInputStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[10240];
for (int length = 0; (length = input.read(buffer)) > 0;) 
  output.write(buffer, 0, length);