Integrate JavaFX and Hibernate
create table Contact(id INT(255) NOT NULL AUTO_INCREMENT PRIMARY KEY, contactinfo VARCHAR(25) NOT NULL);
//hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/javafxpersistence</property>
<property name="hibernate.connection.username">yourusername</property>
<property name="hibernate.connection.password">yourpassword</property>
<mapping resource="contact.hbm.xml">
</mapping>
</session-factory>
</hibernate-configuration></blockquote>
//contact.hbm.xml
<hibernate-mapping>
<class name="com.anro.javafxhibernate.Contact" table="Contact">
<id name="id" type="long" column="ID">
<generator class="assigned">
</generator>
<property name="contactinfo">
<column name="CONTACTINFO">
</column>
</property>
</id>
</hibernate-mapping>
//Contact.java
package com.anro.javafxhibernate;
/**
*
* @author mark anro silva
*/
public class Contact {
private String contactinfo;
private long id;
private Contact() {
}
public Contact(String contactinfo) {
this.contactinfo = contactinfo;
}
public String getContactinfo() {
return contactinfo;
}
public void setContactinfo(String contactinfo) {
this.contactinfo = contactinfo;
}
public long getId() {
return id;
}
public void setId(long l) {
id = l;
}
}
/*
* Main.fx
*
*/
package com.anro.javafxhibernate;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.ext.swing.SwingButton;
import javafx.scene.control.TextBox;
import javafx.scene.input.MouseEvent;
import com.anro.javafxhibernate.Contact;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import javafx.scene.paint.Color;
/**
* @author mark anro silva
*/
var sessionFactory: SessionFactory = new Configuration().configure().buildSessionFactory();
var session: Session = sessionFactory.openSession();
var tx: Transaction = session.beginTransaction();
var label = Text {
translateY: 20
translateX: 10
font: Font { size: 16 }
content: "Contact Info:"
}
var flash = Text {
translateY: 70
translateX: 50
font: Font { size: 14 }
fill: Color.RED
}
var textbox = TextBox {
translateY: 30
translateX: 10
columns: 35
editable: true
}
var button = SwingButton {
translateY: 60
translateX: 10
text: "Save"
onMouseClicked: function (e: MouseEvent): Void {
var message: Contact = bind new Contact(textbox.text);
session.save(message);
tx.commit();
session.close();
textbox.text = "";
flash.content = "Contact Saved";
}
}
Stage {
title: "Save Contact"
scene: Scene {
width: 300
height: 250
content: [label, textbox, button, flash]
}
}
Muito bom tuto... simples e pratico!
ReplyDelete