Java Tutorial/JPA/Column — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 05:18, 1 июня 2010

Change Default Column Name

File: Student.java



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
public class Student {
  @Id
  @Column(name = "EMP_ID")
  private int id = 0;
  @Column(name = "COMM")
  private String name;
  
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String toString() {
    return "\n\nID:" + id + "\nName:" + name + "\n\n";
  }
}





Compound Primary key

File: Student.java



import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
@Entity
@IdClass(StudentId.class)
public class Student {
    @Id private String country;
    @Id
    @Column(name="EMP_ID")
    private int id;
    private String name;

    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    public String toString() {
        return "Student id: " + getId() + " name: " + getName() +
               " country: " + getCountry();
    }
}
class StudentId implements Serializable {
    private String country; 
    private int id; 
    public StudentId() {}
    public StudentId(String country, int id) {
      this.country = country;
      this.id = id;
    }
    public String getCountry() { return country; }
    public int getId() { return id; }
    public boolean equals(Object o) { 
        return ((o instanceof StudentId) && 
                country.equals(((StudentId)o).getCountry()) &&
                id == ((StudentId) o).getId());
    }
    public int hashCode() { 
        return country.hashCode() + id; 
    }
}





Java Int Mapped To Integer

File: Student.java



import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Student {
  @Id
  private int id = 0;
  private String name;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String toString() {
    return "\n\nID:" + id + "\nName:" + name + "\n\n";
  }
}





Java Long Mapped To Big Int

File: Student.java



import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
@Entity
public class Student {
  @Id
  private long id = 0;

  private String name;
  private Date dateOfBirth = new Date();
  @Enumerated(EnumType.STRING)
  private Gender gender;
  
  public Date getDateOfBirth() {
    return dateOfBirth;
  }
  public void setDateOfBirth(Date dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
  }
  public long getId() {
    return id;
  }
  public void setId(long id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public Gender getGender() {
    return gender;
  }
  public void setGender(Gender gender) {
    this.gender = gender;
  }
  
  
}





Java String Mapped To Varchar

File: Gender.java



public enum Gender {
  MALE, FEMALE
}





Set Column Length

File: Employee.java



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Employee {
    @Id 
    @Column(length=40) 
    private String name;

    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
}





Set Precision And Scale

File: Employee.java



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
    @Id private int id;
    private String name;
    @Column(precision=8, scale=2) 
    private float hourlyRate;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    public float getHourlyRate() {
        return hourlyRate;
    }
    public void setHourlyRate(float salary) {
        this.hourlyRate = salary;
    }
}





Specify Column Definition To Field

File: Employee.java



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Employee {
    @Id
    @Column(name="START_DATE", columnDefinition="DATE DEFAULT CURRENT_DATE") 
    private java.sql.Date startDate;
    

}





Unique, Not Nullable Column

File: Employee.java



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
    @Id private int id;
    @Column(unique=true, nullable=false) 
    private String name;
    private long salary;
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public long getSalary() {
        return salary;
    }
    
    public void setSalary(long salary) {
        this.salary = salary;
    }
    
    public String toString() {
        return "Employee id: " + getId() + " name: " + getName();
    }
}





Use Varchar Type As Column Type

File: Employee.java



import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Employee {
    @Id
    @Column(columnDefinition="VARCHAR(40)") 
    private String name;

    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

}