Java Tutorial/Hibernate/Column

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

Combined Columns With Error (Use MySQL)

File: CD.java



import java.util.Date;
public class CD {
  String id;
  String title;
  String artist;
  Date purchaseDate;
  double cost;
  String fullName;
  public CD() {
  }
  public String getFullName() {
    return fullName;
  }
  public void setFullName(String fullName) {
    this.fullName = fullName;
  }
  public Date getPurchaseDate() {
    return purchaseDate;
  }
  public void setPurchaseDate(Date purchaseDate) {
    this.purchaseDate = purchaseDate;
  }
  public CD(String title, String artist, Date purchaseDate, double cost) {
    this.title = title;
    this.artist = artist;
    this.purchaseDate = purchaseDate;
    this.cost = cost;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getId() {
    return id;
  }
  public void setTitle(String title) {
    this.title = title;
  }
  public String getTitle() {
    return title;
  }
  public void setArtist(String artist) {
    this.artist = artist;
  }
  public String getArtist() {
    return artist;
  }
  public void setPurchasedate(Date purchaseDate) {
    this.purchaseDate = purchaseDate;
  }
  public Date getPurchasedate() {
    return purchaseDate;
  }
  public void setCost(double cost) {
    this.cost = cost;
  }
  public double getCost() {
    return cost;
  }
}





Set Column Type In Mapping File

File: Main.java



import java.util.Date;
import org.hibernate.Session;
public class Main {
  public static void main(String[] args) throws Exception {
    HibernateUtil hibernateUtil = new HibernateUtil();
    hibernateUtil.executeSQLCommand("create table survey (id int,name varchar, purchasedate date);");
    Session session = hibernateUtil.getSession();
    Survey survey = new Survey();
    survey.setName("Survey");
    survey.setPurchaseDate(new Date());
    System.out.println(survey.getId());
    
    session.save(survey);
    session.flush();
    
    System.out.println(survey.getId());
    Survey surveyInSession = (Survey) session.get(Survey.class, survey.getId());
    System.out.println(surveyInSession.getName());
    session.close();
    hibernateUtil.checkData("select * from survey");
  }
}