1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| public class Exemples {
public static class HibernateExample {
public static void main(String[] args) {
// 1. Add the new terme
//addTerme();
// 2. Retrieve the terme and print it
listTerme();
//find();
// 3. Change the terme record and update it
//changeTerme();
}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 | private static void addTerme() {
// 1. Create terme
Terme terme = new Terme();
terme.setIdTerme(10);
terme.setIdLangue("AU ");
terme.setIdThesaurus(5);
terme.setDescripteur("AREUHHHH");
terme.setNoteApplication("Je sais pas quoi ....");
terme.setNoteHistorique("NON");
terme.setDateCreation(new Date(1982-05-01));
terme.setDateModification(new Date(1989-03-11));
terme.setType("MT");
terme.setIdMt(8);
terme.setTest("Test Bruno");
// 2. Create DAO
TermeDAO dao = new TermeDAO();
// 3. Start the transaction
Transaction tx = dao.getSession().beginTransaction();
// 4. Add user
dao.save(terme);
// 5. Commit the transaction (write to database)
tx.commit();
// 6. Close the session (cleanup connections)
dao.getSession().close();
}
private static void listTerme() {
// 1. Create DAO
TermeDAO dao = new TermeDAO();
// 2. Find terme by ID
Terme terme = dao.findById(2);
// 3. Print the user information out
printTerme("Printing Terme, ", terme);
// 4. Close the session (cleanup connections)
dao.getSession().close();
}
private static void changeTerme() {
// 1. Create DAO
TermeDAO dao = new TermeDAO();
// 2. Find terme by ID
Terme terme = dao.findById(1);
// 3. Change terme information
terme.setDescripteur("jsmith");
terme.setIdLangue("abcd");
terme.setTest("Jane");
// 4. Start the transaction
Transaction tx = dao.getSession().beginTransaction();
// 5. Update the user record with the changes
dao.save(terme);
// 6. Commit the transaction (write to database)
tx.commit();
// 7. Load the updated user from the database
Terme updatedTerme = dao.findById(1);
// 8. Print the updated user information out to confirm the changes
printTerme("Printing Updated Terme, ", updatedTerme);
// 9. Close the session (cleanup connections)
dao.getSession().close();
}
private static void printTerme(String extraText, Terme terme) {
System.out.println(extraText
+ " Terme: "
+ terme.getDescripteur()
+ ", Langue: "
+ terme.getIdLangue()
+ ", Type: "
+ terme.getType()
+ ", Thesaurus: "
+ terme.getIdThesaurus());
}
@SuppressWarnings("unchecked")
private static void find() { |
TermeDAO dao = new TermeDAO();
List terme = dao.findByExpression("descripteur", "e");
if (terme.size() > 0) {
for(Iterator iter = terme.iterator(); iter.hasNext();)
{
System.out.println(iter.next());
}
}
}
}
} |
Partager