[go: up one dir, main page]

Menu

[r285]: / html / tutorial.html  Maximize  Restore  History

Download this file

1186 lines (1169 with data), 62.1 kB

   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
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Conserve - tutorial</title>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
<meta name="ROBOTS" content="INDEX,FOLLOW">
<meta http-equiv="PRAGMA" content="NO-CACHE">
<meta name="KEYWORDS"
content="java, jdbc, orm, database, tutorial, conserve">
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
<!-- Piwik -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://sourceforge.net/apps/piwik/conserve/" : "http://sourceforge.net/apps/piwik/conserve/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {
}
</script>
<style type="text/css">
table.newcolor {
background-color: #e1ffe0;
width: 100%;
text-align: left;
border-width: 0px;
border-spacing: 0px;
border: 0px;
padding: 0px;
}
table.warningtable {
background-color: #e1e1e1;
width: 100%;
text-align: left;
border-width: 0px;
border-spacing: 0px;
border: 0px;
padding: 0px;
}
</style>
</head>
<body>
<noscript>
<p> <img
src="http://sourceforge.net/apps/piwik/conserve/piwik.php?idsite=1"
style="border: 0pt none ;" alt=""> </p>
</noscript>
<table summary=""
style="text-align: left; width: 900px; margin-left: auto; margin-right: auto;"
border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;"> <a
href="http://conserve.sourceforge.net/"> <img
style="border: 0px solid ; width: 238px; height: 76px;"
alt="Conserve logo" title="Conserve logo" src="img/logo.png"> </a>
<h2> <big> <span style="font-weight: bold;">Tutorial</span> </big>
</h2>
The purpose of this tutorial is to get you up and running with Conserve
in the shortest amount of time. <br>
After completing this tutorial you should know the basics of using
Conserve in your project. <br>
<br>
The Advanced section is not really necessary for using Conserve, but if
you decide to read it you'll gain <br>
a better understanding of Conserve and learn to do some neat stuff. <br>
<hr style="width: 100%; height: 2px;">
<h3>Contents:</h3>
<a href="tutorial.html#whatyouneed">What you need</a> <br>
<a href="tutorial.html#gettingstarted">Getting started</a> <br>
<a href="tutorial.html#createmanager"> <span
style="text-decoration: underline;">Create a PersistenceManager</span>
</a> <br>
<a href="tutorial.html#save">Save an object</a> <br>
<a href="tutorial.html#search"> <span
style="text-decoration: underline;">Search for objects</span> </a> <br>
<a href="tutorial.html#sorting">Sorting and limits</a> <br>
<a href="tutorial.html#delete"> <span
style="text-decoration: underline;">Delete objects</span> </a> <br>
<a href="tutorial.html#updateobjects">Update objects</a> <br>
<a href="tutorial.html#advanced"> <span
style="text-decoration: underline;">Advanced: </span> </a> <br>
<div style="margin-left: 40px;"> <a
href="tutorial.html#forcerefresh">Forcing refresh of existing objects</a>
</div>
<div style="margin-left: 40px;"> <a
href="tutorial.html#droptables"> <span
style="text-decoration: underline;">Drop tables</span> </a> </div>
<div style="margin-left: 40px;"> <a
href="tutorial.html#duplicate">Duplicate database</a> <br>
<a href="tutorial.html#ignored">Ignored object properties</a> <br>
<a href="tutorial.html#customtablenames">Custom table names</a> <br>
<a href="tutorial.html#customcolumnnames">Custom column names</a>
<br>
<a href="tutorial.html#customstringlength">Custom string length</a>
<br>
<a href="tutorial.html#usingclobsandblobs">Using CLOBs and BLOBs</a>
<br>
<a href="tutorial.html#indexes">Indices</a> </div>
<hr style="width: 100%; height: 2px;">
<h3> <a id="whatyouneed">What you need: </a> </h3>
<ul>
<li> The Conserve .jar file. You can <a
href="http://sourceforge.net/projects/conserve/files/">download the
latest binary from the SourceForge project page.</a>&nbsp; </li>
<li> An SQL database compatible with Conserve. The <a
href="http://conserve.sourceforge.net/#compability">list of compatible
databases can be found here.</a> For this tutorial we will be using <a
href="http://www.sqlite.org/">SQLite</a>. </li>
<li> Read/Write permission to the database. </li>
<li> The <a href="http://en.wikipedia.org/wiki/JDBC_driver">JDBC
driver</a> for your particular database. This should be available from
the database vendor's website. If not, try googling "JDBC" + the name
of your database engine. </li>
<li> If your database requires a login/password, you'll need
that too. </li>
</ul>
<br>
<h3> <a id="gettingstarted">Getting started: </a> </h3>
As mentioned above, we will be using SQLite. This is because its
installation is so simple, which lets us concentrate on the important
aspects of Conserve instead of how to install and configure the
database engine. <br>
<br>
Follow these steps: <br>
1. Download the <a
href="http://sourceforge.net/projects/conserve/files/">Conserve .jar
file</a> and the <a href="http://www.zentus.com/sqlitejdbc/">SQLite
JDBC driver</a> jar file. <br>
2. Add both jar files to the classpath. There are several ways of doing
this, the simplest is to copy the files to the
$JRE_HOME/lib/ext/folder. <br>
<br>
That's it, you've now installed Conserve and SQLite. <br>
<br>
<h3> <a id="createmanager">Create a PersistenceManager: </a> </h3>
Now that everything is installed, we're ready to start programming with
Conserve. <br>
Just fire up your favourite code editor and create a new Java class. <br>
<br>
Then, import the Conserve library: <br>
<table class="newcolor" summary="">
<tbody>
<tr>
<td>
<pre><tt><b><font color="#000080">import</font></b> org<font
color="#990000">.</font>conserve<font color="#990000">.</font>PersistenceManager<font
color="#990000">;</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
Inside a method body, create an instance of the PersistenceManager: <br>
<table summary="" style="text-align: left;" class="newcolor"
border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><font color="#008080">PersistenceManager</font> pm <font
color="#990000">=</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">PersistenceManager</font></b><font color="#990000">(</font><font
color="#ff0000">"org.sqlite.JDBC"</font><font color="#990000">,</font><font
color="#ff0000">"jdbc:sqlite:tutorial.db"</font><font color="#990000">,</font> <font
color="#ff0000">"myuser"</font><font color="#990000">,</font> <font
color="#ff0000">"mypassword"</font><font color="#990000">);</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
That's it! The <span style="font-family: monospace;">"org.sqlite.JDBC"</span>string
is
the class name of the JDBC driver. If you are using a JDBC version
4.0 or better driver, you can omit this. We've only included it here
because SQLite does not have a version 4.0 driver. <br>
<br>
The string <span style="font-family: monospace;">"jdbc:sqlite:tutorial.db"</span>is
the
connection string. It contains the name of the database you wishto
use. Connection strings are database dependent, so we won't go into
detail about them here. You can find your connection string in
thedocumentation for your JDBC driver. <br>
<br>
The <span style="font-family: monospace;">"myuser"</span> and <span
style="font-family: monospace;">"mypassword"</span> strings are the
user name and password, respectively, that you use to authenticate to
the database. <br>
<br>
<span style="font-weight: bold;">Important! </span>When you are
done with the PersistenceManager, make sure to close it. This cleans up
any resources allocated to by the manager and ensures there are no
uncommitted changes. This is done easily like this: <br>
<table summary="" class="newcolor" border="0" cellpadding="0"
cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>pm<font color="#990000">.</font><b><font
color="#000000">close</font></b><font color="#990000">();</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
The whole class looks like this:
<table summary="" class="newcolor" border="0" cellpadding="0"
cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><b><font color="#000080">import</font></b> java<font
color="#990000">.</font>sql<font color="#990000">.</font>SQLException<font
color="#990000">;</font><br><br><b><font color="#000080">import</font></b> org<font
color="#990000">.</font>conserve<font color="#990000">.</font>PersistenceManager<font
color="#990000">;</font><br><br><i><font color="#9a1900">/**</font></i><br><i><font
color="#9a1900"> * </font></i><font color="#009900">@author</font><i><font
color="#9a1900"> <span style="font-family: monospace;">Your Name</span></font></i><br><i><font
color="#9a1900"> *</font></i><br><i><font color="#9a1900"> */</font></i><br><b><font
color="#0000ff">public</font></b> <b><font color="#0000ff">class</font></b> <font
color="#008080">ConserveTutorial</font><br><font color="#ff0000">{</font><br><br> <i><font
color="#9a1900">/**</font></i><br><i><font color="#9a1900"> * </font></i><font
color="#009900">@param</font><i><font color="#9a1900"> args</font></i><br><i><font
color="#9a1900"> * </font></i><font color="#009900">@throws</font><i><font
color="#9a1900"> SQLException </font></i><br><i><font color="#9a1900"> */</font></i><br> <b><font
color="#0000ff">public</font></b> <b><font color="#0000ff">static</font></b> <font
color="#009900">void</font> <b><font color="#000000">main</font></b><font
color="#990000">(</font>String<font color="#990000">[]</font> args<font
color="#990000">)</font> <b><font color="#0000ff">throws</font></b> SQLException<br> <font
color="#ff0000">{</font><br> <font color="#008080">PersistenceManager</font> pm <font
color="#990000">=</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">PersistenceManager</font></b><font color="#990000">(</font><font
color="#ff0000">"org.sqlite.JDBC"</font><font color="#990000">,</font><font
color="#ff0000">"jdbc:sqlite:tutorial.db"</font><font color="#990000">,</font> <font
color="#ff0000">"myuser"</font><font color="#990000">,</font> <font
color="#ff0000">"mypassword"</font><font color="#990000">);</font><br> pm<font
color="#990000">.</font><b><font color="#000000">close</font></b><font
color="#990000">();</font><br> <font color="#ff0000">}</font><br><font
color="#ff0000">}</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
If you paste this code into a .java file, compile and run it you should
find a file named "tutorial.db" in the current directory. <br>
That's the file SQLite uses for permanent storage of your database. <br>
If you open the file you'll see the tables Conserve has created for its
internal book-keeping. <br>
<br>
Note that the <span style="font-family: monospace;">main()</span>method
throws <span style="font-family: monospace;">SQLException</span>.
Normally you'd want to catch and handle this exception in your
application. <br>
<br>
In the rest of the tutorial we will omit complete code listings and
instead focus on just the parts of the code we're interested in. <br>
<h3> <a id="save">Save an object:</a> </h3>
Now that you have a PersistenceManager object, you can save objects.
Let's create an instance of a simple data class and store it. We'll use
an object of a class called <a href="TextObject.java" target="_blank">TextObject.</a>
<br>
<br>
All we have to do is create the object, fill in some data and save it:
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><font color="#008080">TextObject</font> a <font
color="#990000">=</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">TextObject</font></b><font color="#990000">();</font><br>a<font
color="#990000">.</font><b><font color="#000000">setText</font></b><font
color="#990000">(</font><font color="#ff0000">"The quick brown fox"</font><font
color="#990000">);</font><br>a<font color="#990000">.</font><b><font
color="#000000">setKeyWords</font></b><font color="#990000">(</font>a<font
color="#990000">.</font><b><font color="#000000">getText</font></b><font
color="#990000">().</font><b><font color="#000000">split</font></b><font
color="#990000">(</font><font color="#ff0000">" "</font><font
color="#990000">));</font><br>pm<font color="#990000">.</font><b><font
color="#000000">saveObject</font></b><font color="#990000">(</font>a<font
color="#990000">);</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
<span style="font-weight: bold;"> </span>That's all there is to
it, really. Conserve handles creating a connection, checking if the
table exists, creating a transaction and committing the transaction
when you're done. If you want to handle transactions yourself, you can
do that also: <br>
<table class="newcolor" style="text-align: left; width: 100%;"
summary="" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><font color="#008080">ConnectionWrapper</font> cw <font
color="#990000">=</font> pm<font color="#990000">.</font><b><font
color="#000000">getConnectionWrapper</font></b><font color="#990000">();</font><br><font
color="#008080">TextObject</font> a <font color="#990000">=</font> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">TextObject</font></b><font
color="#990000">();</font><br>a<font color="#990000">.</font><b><font
color="#000000">setText</font></b><font color="#990000">(</font><font
color="#ff0000">"The quick brown fox"</font><font color="#990000">);</font><br>a<font
color="#990000">.</font><b><font color="#000000">setKeyWords</font></b><font
color="#990000">(</font>a<font color="#990000">.</font><b><font
color="#000000">getText</font></b><font color="#990000">().</font><b><font
color="#000000">split</font></b><font color="#990000">(</font><font
color="#ff0000">" "</font><font color="#990000">));</font><br>pm<font
color="#990000">.</font><b><font color="#000000">saveObject</font></b><font
color="#990000">(</font>cw<font color="#990000">,</font>a<font
color="#990000">);</font><br>cw<font color="#990000">.</font><b><font
color="#000000">commitAndDiscard</font></b><font color="#990000">();</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
Here, we request a <span style="font-family: monospace;">ConnectionWrapper</span>
object from the <span style="font-family: monospace;">PersistenceManager</span>.
This lets us tell Conserve that we do not want any changes to be
committed until we call <span style="font-family: monospace;">commit()</span>
or, as in this example, <span style="font-family: monospace;">commitAndDiscard()</span>.<br>
This is sometimes faster, especially if you are doing several similar
insert operations in a row.
<p> After a <span style="font-family: monospace;">ConnectionWrapper</span>
is discarded, it returns to the <span style="font-family: monospace;">PersistenceManager</span>'s
connection pool and should not be used again.</p>
<p> Note that we are here calling a different version of the <span
style="font-family: monospace;">saveObject</span> method, one that
accepts a <span style="font-family: monospace;">ConnectionWrapper</span>
as the first parameter.<br>
You'll find that most of the <span style="font-family: monospace;">PersistenceManager</span>
methods follows this pattern: One version that takes a <span
style="font-family: monospace;">ConnectionWrapper</span> as an
argument and lets you handle committing, and another version that does
not take a <span style="font-family: monospace;">ConnectionWrapper</span>
argument because it handles committing for you. <br>
<br>
</p>
<h3> <a id="search">Search for objects: </a> </h3>
Conserve uses a technique called search-by-example. This means that
instead of manipulating strings, you work with objects. <br>
If you want to find, for example, all objects of a given class
thatshare one particular property, all you have to do is create an
object of this class, set the desired property, and ask Conserve to
find allsimilar objects. <br>
Conserve will match <span style="font-weight: bold;">all non-null
properties</span> of the example object. <br>
<br>
It might be unfamiliar at first (especially if you're used to
concatenating string literals to get the object you want), so it might
be simpler to show an example. <br>
Let's say that we want to retrieve the object we saved earlier. All we
do is create an 'example' object and compose an <span
style="font-family: monospace;">Equal</span> clause with it
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><font color="#008080">TextObject</font> example <font
color="#990000">=</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">TextObject</font></b><font color="#990000">();</font><br>example<font
color="#990000">.</font><b><font color="#000000">setText</font></b><font
color="#990000">(</font><font color="#ff0000">"The quick brown fox"</font><font
color="#990000">);</font><br>List<font color="#990000">&lt;</font>TextObject<font
color="#990000">&gt;</font>res <font color="#990000">=</font> pm<font
color="#990000">.</font><b><font color="#000000">getObjects</font></b><font
color="#990000">(</font>TextObject<font color="#990000">.</font><b><font
color="#0000ff">class</font></b><font color="#990000">,</font> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">Equal</font></b><font
color="#990000">(</font>example<font color="#990000">));</font><br>System<font
color="#990000">.</font>out<font color="#990000">.</font><b><font
color="#000000">println</font></b><font color="#990000">(</font>res<font
color="#990000">.</font><b><font color="#000000">get</font></b><font
color="#990000">(</font><font color="#993399">0</font><font
color="#990000">).</font><b><font color="#000000">getKeyWords</font></b><font
color="#990000">());</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<span> <br>
The above code will print out the string "[The, fox, quick, brown]",
which was retrieved from the database. <br>
Note that Conserve did not match up the keywords when selecting, as the
example's keywords were empty. <br>
<br>
Speaking of keywords: The <span style="font-family: monospace;">getKeyWords()</span>
method returns a <span style="font-family: monospace;">Set</span>, and
<span style="font-family: monospace;">Sets</span> are unordered.
This means that Conserve will return any object that has all the
keywords that the example has, no matter what order they are given in: </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>example<font color="#990000">.</font><b><font
color="#000000">setText</font></b><font color="#990000">(</font><b><font
color="#0000ff">null</font></b><font color="#990000">);</font><br>example<font
color="#990000">.</font><b><font color="#000000">setKeyWords</font></b><font
color="#990000">(</font><b><font color="#0000ff">new</font></b> String<font
color="#990000">[]</font><font color="#ff0000">{</font><font
color="#ff0000">"brown"</font><font color="#ff0000">}</font><font
color="#990000">);</font><br>res <font color="#990000">=</font> pm<font
color="#990000">.</font><b><font color="#000000">getObjects</font></b><font
color="#990000">(</font>TextObject<font color="#990000">.</font><b><font
color="#0000ff">class</font></b><font color="#990000">,</font> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">Equal</font></b><font
color="#990000">(</font>example<font color="#990000">));</font><br>System<font
color="#990000">.</font>out<font color="#990000">.</font><b><font
color="#000000">println</font></b><font color="#990000">(</font>res<font
color="#990000">.</font><b><font color="#000000">get</font></b><font
color="#990000">(</font><font color="#993399">0</font><font
color="#990000">).</font><b><font color="#000000">getText</font></b><font
color="#990000">());</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<span> <br>
This will print out the string "The quick brown fox". <br>
<br>
There are several clause classes besides <span
style="font-family: monospace;">Equal</span>, and they all pretty much
do what they say on the tin. <span style="font-family: monospace;">Different</span>,
for example, finds all objects that do not match the given example
object: </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>res <font color="#990000">=</font> pm<font
color="#990000">.</font><b><font color="#000000">getObjects</font></b><font
color="#990000">(</font>TextObject<font color="#990000">.</font><b><font
color="#0000ff">class</font></b><font color="#990000">,</font> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">Different</font></b><font
color="#990000">(</font>a<font color="#990000">));</font><br><b><font
color="#0000ff">if</font></b><font color="#990000">(</font>res<font
color="#990000">.</font><b><font color="#000000">size</font></b><font
color="#990000">()==</font><font color="#993399">0</font><font
color="#990000">)</font><br><font color="#ff0000">{</font><br> System<font
color="#990000">.</font>out<font color="#990000">.</font><b><font
color="#000000">println</font></b><font color="#990000">(</font><font
color="#ff0000">"No matches found."</font><font color="#990000">);</font><br><font
color="#ff0000">}</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<span> <br>
You can combine several clauses using the <span
style="font-family: monospace;">And</span> and <span
style="font-family: monospace;">Or</span> classes. There is a special
clause called <span style="font-family: monospace;">All</span>, which
simply returns all objects of a given class. <br>
<br>
Using the search-by-example approach makes it very simple to create
complex queries - Conserve will generate the appropriate joins and
sub-queries for you. Here is how you find all books by authors that
have written about crime (The <span style="font-family: monospace;">Author
</span>and <span style="font-family: monospace;">Book</span>
classes are from the Conserve test suite): </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><font color="#008080">Author</font> crimeAuthor <font
color="#990000">=</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">Author</font></b><font color="#990000">();</font><br><font
color="#008080">Book</font> crimeBook <font color="#990000">=</font> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">Book</font></b><font
color="#990000">();</font><br>crimeBook<font color="#990000">.</font><b><font
color="#000000">addKeyWord</font></b><font color="#990000">(</font><font
color="#ff0000">"crime"</font><font color="#990000">);</font><br>crimeAuthor<font
color="#990000">.</font><b><font color="#000000">addBook</font></b><font
color="#990000">(</font>crimeBook<font color="#990000">);</font><br><font
color="#008080">Book</font> seekBook <font color="#990000">=</font> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">Book</font></b><font
color="#990000">();</font><br>seekBook<font color="#990000">.</font><b><font
color="#000000">addAuthor</font></b><font color="#990000">(</font>crimeAuthor<font
color="#990000">);</font><br><font color="#008080">List&lt;Book&gt;</font> crimeAuthorBooks <font
color="#990000">=</font> persist<font color="#990000">.</font><b><font
color="#000000">getObjects</font></b><font color="#990000">(</font>Book<font
color="#990000">.</font><b><font color="#0000ff">class</font></b><font
color="#990000">,</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">Equal</font></b><font color="#990000">(</font>seekBook<font
color="#990000">));</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<span> <br>
Conserve also lets you search class hierarchies. <br>
<br>
Let's say we have two classes, <span style="font-family: monospace;">FooTextObject
</span>and <span style="font-family: monospace;">BarTextObject</span>,
who are both direct subclasses of <span style="font-family: monospace;">TextObject</span>.
<br>
We insert one object each of <span style="font-family: monospace;">FooTextObject</span>,
<span style="font-family: monospace;">BarTextObject</span> and <span
style="font-family: monospace;">TextObject</span> into the database: </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><font color="#008080">TextObject</font> a <font
color="#990000">=</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">TextObject</font></b><font color="#990000">();</font><br>a<font
color="#990000">.</font><b><font color="#000000">setText</font></b><font
color="#990000">(</font><font color="#ff0000">"The quick brown fox"</font><font
color="#990000">);</font><br>a<font color="#990000">.</font><b><font
color="#000000">setKeyWords</font></b><font color="#990000">(</font>a<font
color="#990000">.</font><b><font color="#000000">getText</font></b><font
color="#990000">().</font><b><font color="#000000">split</font></b><font
color="#990000">(</font><font color="#ff0000">" "</font><font
color="#990000">));</font><br>pm<font color="#990000">.</font><b><font
color="#000000">saveObject</font></b><font color="#990000">(</font>a<font
color="#990000">);</font><br><font color="#008080">FooTextObject</font> foo <font
color="#990000">=</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">FooTextObject</font></b><font color="#990000">();</font><br>foo<font
color="#990000">.</font><b><font color="#000000">setKeyWords</font></b><font
color="#990000">(</font><b><font color="#0000ff">new</font></b> String<font
color="#990000">[]</font> <font color="#ff0000">{</font> <font
color="#ff0000">"The"</font><font color="#990000">,</font> <font
color="#ff0000">"quick"</font><font color="#990000">,</font> <font
color="#ff0000">"brown"</font><font color="#990000">,</font> <font
color="#ff0000">"dog"</font> <font color="#ff0000">}</font><font
color="#990000">);</font><br>pm<font color="#990000">.</font><b><font
color="#000000">saveObject</font></b><font color="#990000">(</font>foo<font
color="#990000">);</font><br><font color="#008080">BarTextObject</font> bar <font
color="#990000">=</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">BarTextObject</font></b><font color="#990000">();</font><br>bar<font
color="#990000">.</font><b><font color="#000000">setKeyWords</font></b><font
color="#990000">(</font><b><font color="#0000ff">new</font></b> String<font
color="#990000">[]</font> <font color="#ff0000">{</font> <font
color="#ff0000">"The"</font><font color="#990000">,</font> <font
color="#ff0000">"quick"</font><font color="#990000">,</font> <font
color="#ff0000">"brown"</font><font color="#990000">,</font> <font
color="#ff0000">"rabbit"</font> <font color="#ff0000">}</font><font
color="#990000">);</font><br>pm<font color="#990000">.</font><b><font
color="#000000">saveObject</font></b><font color="#990000">(</font>bar<font
color="#990000">);</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<span> <br>
Now we can find all instances of <span style="font-family: monospace;">TextObject</span>
or any of its subclasses that contain the keyword "brown": </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><font color="#008080">TextObject</font> example <font
color="#990000">=</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">TextObject</font></b><font color="#990000">();</font><br>example<font
color="#990000">.</font><b><font color="#000000">setKeyWords</font></b><font
color="#990000">(</font><b><font color="#0000ff">new</font></b> String<font
color="#990000">[]</font> <font color="#ff0000">{</font> <font
color="#ff0000">"brown"</font> <font color="#ff0000">}</font><font
color="#990000">);</font><br><font color="#008080">List&lt;TextObject&gt;</font> list <font
color="#990000">=</font> pm<font color="#990000">.</font><b><font
color="#000000">getObjects</font></b><font color="#990000">(</font>TextObject<font
color="#990000">.</font><b><font color="#0000ff">class</font></b><font
color="#990000">,</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">Equal</font></b><font color="#990000">(</font>example<font
color="#990000">));</font><br>System<font color="#990000">.</font>out<font
color="#990000">.</font><b><font color="#000000">println</font></b><font
color="#990000">(</font>list<font color="#990000">.</font><b><font
color="#000000">size</font></b><font color="#990000">());</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<span> <br>
This code should output "3". <br>
<br>
We can find all instances of <span style="font-family: monospace;">FooTextObject</span>
that contain the keyword "brown": </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><font color="#008080">List&lt;FooTextObject&gt;</font> fooList <font
color="#990000">=</font> pm<font color="#990000">.</font><b><font
color="#000000">getObjects</font></b><font color="#990000">(</font>FooTextObject<font
color="#990000">.</font><b><font color="#0000ff">class</font></b><font
color="#990000">,</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">Equal</font></b><font color="#990000">(</font>example<font
color="#990000">));</font><br>System<font color="#990000">.</font>out<font
color="#990000">.</font><b><font color="#000000">println</font></b><font
color="#990000">(</font>fooList<font color="#990000">.</font><b><font
color="#000000">size</font></b><font color="#990000">());</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<span> <br>
This code should output "1". <br>
<br>
We can also find all instances of <span style="font-family: monospace;">TextObject</span>
that contain the keyword "rabbit": </span>
<table summary=""
style="text-align: left; width: 896px; height: 73px;" class="newcolor"
border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>example<font color="#990000">.</font><b><font
color="#000000">setKeyWords</font></b><font color="#990000">(</font><b><font
color="#0000ff">new</font></b> String<font color="#990000">[]</font> <font
color="#ff0000">{</font> <font color="#ff0000">"rabbit"</font> <font
color="#ff0000">}</font><font color="#990000">);</font><br><font
color="#008080">List&lt;TextObject&gt;</font> rabbitList <font
color="#990000">=</font> pm<font color="#990000">.</font><b><font
color="#000000">getObjects</font></b><font color="#990000">(</font>TextObject<font
color="#990000">.</font><b><font color="#0000ff">class</font></b><font
color="#990000">,</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">Equal</font></b><font color="#990000">(</font>example<font
color="#990000">));</font><br>System<font color="#990000">.</font>out<font
color="#990000">.</font><b><font color="#000000">println</font></b><font
color="#990000">(</font>rabbitList<font color="#990000">.</font><b><font
color="#000000">size</font></b><font color="#990000">());</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
This code should output "1". <br>
<br>
We've barely scratched the surface of what Conserve can achieve with
searches. For example, the inheritance-based search demonstrated above
also works with interfaces. <br>
There are also relational classes for relations besides <span
style="font-family: monospace;">Equal</span>: <span
style="font-family: monospace;">All</span>, <span
style="font-family: monospace;">Different</span>, <span
style="font-family: monospace;">Greater</span>, <span
style="font-family: monospace;">GreaterOrEqual</span>, <span
style="font-family: monospace;">Less</span>, <span
style="font-family: monospace;">LessOrEqual</span>, <span
style="font-family: monospace;">Null</span>, and <span
style="font-family: monospace;">NotNull</span>. They can be combined
in anyway you like with the <span style="font-family: monospace;">And</span>
and <span style="font-family: monospace;">Or</span> classes.<br>
Instead of boring you with lots of examples, we'll leave you to
experiment. Have fun! <br>
<h3> <span> <a id="sorting">Sorting and limits:</a> </span> </h3>
<span> Sorting is done by passing objects of the type <span
style="font-family: monospace;">Order</span> <span
style="font-weight: bold;"> </span>to the <span
style="font-family: monospace;">getObjects</span> method. There are
two to choose from: <span style="font-family: monospace;">Ascending</span>
(which sorts ascending) and <span style="font-family: monospace;">Descending</span>
<span style="font-weight: bold;"> </span>(which -- surprise --
sorts descending).&nbsp; These objects sort based on the non-null
properties of the example object you pass to them, just like the
relational classes discussed in the previous section. <br>
This example shows you how to sort <a href="Person.java">Person</a>
objects descending by last name:
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><i><font color="#9a1900">// create the sorting object</font></i>
<font color="#008080">Person</font> lastNameSort <font color="#990000">=</font> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">Person</font></b><font
color="#990000">();</font>
<i><font color="#9a1900">// the value of LastName does not matter, only that it is not null</font></i>
lastNameSort<font
color="#990000">.</font><b><font color="#000000">setLastName</font></b><font
color="#990000">(</font><font color="#ff0000">"foo"</font><font
color="#990000">);</font>
<i><font color="#9a1900">// sort descending by last name</font></i>
<font color="#008080">List&lt;Person&gt;</font> res <font
color="#990000">=</font> pm<font color="#990000">.</font><b><font
color="#000000">getObjects</font></b><font color="#990000">(</font>Person<font
color="#990000">.</font><b><font color="#0000ff">class</font></b><font
color="#990000">,</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">All</font></b><font color="#990000">(),</font> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">Descending</font></b><font
color="#990000">(</font>lastNameSort<font color="#990000">));</font>
<i><font color="#9a1900">// print the results</font></i>
<b><font color="#0000ff">for</font></b> <font color="#990000">(</font><font
color="#008080">Person</font> p <font color="#990000">:</font> res<font
color="#990000">)</font>
System<font color="#990000">.</font>out<font
color="#990000">.</font><b><font color="#000000">println</font></b><font
color="#990000">(</font>p<font color="#990000">.</font><b><font
color="#000000">getLastName</font></b><font color="#990000">()</font> <font
color="#990000">+</font> <font color="#ff0000">", "</font> <font
color="#990000">+</font> p<font color="#990000">.</font><b><font
color="#000000">getFirstName</font></b><font color="#990000">());</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
Any number of <span style="font-family: monospace;">Ascending</span> <span
style="font-weight: bold;"> </span>or <span
style="font-family: monospace;">Descending</span> <span
style="font-weight: bold;"> </span>objects can be grouped in an <span
style="font-family: monospace;">Order</span> <span
style="font-weight: bold;"> </span>object. This snippet shows you how
to sort <a href="Person.java">Person</a> objects descending according
to lastname, then ascending according to firstname: </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><i><font color="#9a1900">//create the sorting objects</font></i>
<font color="#008080">Person</font> lastNameSort <font color="#990000">=</font> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">Person</font></b><font
color="#990000">();</font>
<font color="#008080">Person</font> firstNameSort <font color="#990000">=</font> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">Person</font></b><font
color="#990000">();</font>
<i><font color="#9a1900">//the values of LastName and FirstName do not matter, only that they are not null</font></i>
lastNameSort<font
color="#990000">.</font><b><font color="#000000">setLastName</font></b><font
color="#990000">(</font><font color="#ff0000">"foo"</font><font
color="#990000">);</font>
firstNameSort<font color="#990000">.</font><b><font
color="#000000">setFirstName</font></b><font color="#990000">(</font><font
color="#ff0000">"bar"</font><font color="#990000">);</font>
<i><font color="#9a1900">//sort descending by last name, then ascending by first name</font></i>
<font color="#008080">List&lt;Person&gt;</font> res <font
color="#990000">=</font> pm<font color="#990000">.</font><b><font
color="#000000">getObjects</font></b><font color="#990000">(</font>Person<font
color="#990000">.</font><b><font color="#0000ff">class</font></b><font
color="#990000">,</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">All</font></b><font color="#990000">(),</font> <br> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">Order</font></b><font
color="#990000">(</font><b><font color="#0000ff">new</font></b> <b><font
color="#000000">Descending</font></b><font color="#990000">(</font>lastNameSort<font
color="#990000">),</font><b><font color="#0000ff">new</font></b> <b><font
color="#000000">Ascending</font></b><font color="#990000">(</font>firstNameSort<font
color="#990000">)));</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
<span> An <span style="font-family: monospace;">Order</span> <span
style="font-weight: bold;"> </span>object can also impose limit and
offset constraints. Limit means that you only get a limited number of
results. This is how you get only the first three results: </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><font color="#008080">List&lt;Person&gt;</font> res <font
color="#990000">=</font> pm<font color="#990000">.</font><b><font
color="#000000">getObjects</font></b><font color="#990000">(</font>Person<font
color="#990000">.</font><b><font color="#0000ff">class</font></b><font
color="#990000">,</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">All</font></b><font color="#990000">(),</font> <br> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">Order</font></b><font
color="#990000">(</font> <font color="#993399">3</font><font
color="#990000">,</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">Ascending</font></b><font color="#990000">(</font>firstNameSort<font
color="#990000">),</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">Ascending</font></b><font color="#990000">(</font>lastNameSort<font
color="#990000">)));</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<span> <br>
Offset means that you skip a number of results at the beginning. This
is how you skip the first 3 results and get the next 2: </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><font color="#008080">List&lt;Person&gt;</font> res <font
color="#990000">=</font> pm<font color="#990000">.</font><b><font
color="#000000">getObjects</font></b><font color="#990000">(</font>Person<font
color="#990000">.</font><b><font color="#0000ff">class</font></b><font
color="#990000">,</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">All</font></b><font color="#990000">(),</font> <br> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">Order</font></b><font
color="#990000">(</font><font color="#993399">2</font><font
color="#990000">,</font> <font color="#993399">3</font><font
color="#990000">,</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">Ascending</font></b><font color="#990000">(</font>firstNameSort<font
color="#990000">),</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">Ascending</font></b><font color="#990000">(</font>lastNameSort<font
color="#990000">)));</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
<h3> <a id="delete">Delete objects:</a> </h3>
Deleting works exactly like searching, but instead of returning a list
of found objects, the <span style="font-family: monospace;">deleteObjects</span>
family of methods return an integer indicating how many records were
deleted: <br>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt><font color="#009900">int</font> deleted <font
color="#990000">=</font> pm<font color="#990000">.</font><b><font
color="#000000">deleteObjects</font></b><font color="#990000">(</font>TextObject<font
color="#990000">.</font><b><font color="#0000ff">class</font></b><font
color="#990000">,</font> <b><font color="#0000ff">new</font></b> <b><font
color="#000000">All</font></b><font color="#990000">());</font><br>System<font
color="#990000">.</font>out<font color="#990000">.</font><b><font
color="#000000">println</font></b><font color="#990000">(</font><font
color="#ff0000">"Deleted "</font> <font color="#990000">+</font> deleted <font
color="#990000">+</font> <font color="#ff0000">" record(s)"</font><font
color="#990000">);</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
The above example will output "Deleted 1 record(s)", provided there was
one TextObject stored in your database. <br>
<h3> <a id="updateobjects">Update objects: </a> </h3>
Updating an object simply consists of changing its attribute(s) and
saving it again. Conserve will detect that the object already exists
and update the database accordingly. <br>
<br>
Please note that this only works if the <span
style="font-family: monospace;">PersistenceManager</span> you re-save
the object with is the same that you originally loaded it with. If you
re-save the object with a different <span
style="font-family: monospace;">PersistenceManager</span>, a new
object will be created in the database. <br>
<h3> <a id="advanced">Advanced:&nbsp;</a> </h3>
<h3 style="margin-left: 40px;"> <a id="forcerefresh">Forcing
refresh of existing objects: </a> </h3>
<div style="margin-left: 40px;"> <span>Sometimes you want to get
the latest copy of an object from the
database. Conserve's instance consistency feature means that if you
simply execute a new search forthe object, you'll get the copy stored
in memory. If you want to make sure you get the latest version in the
database, you must use <span style="font-family: monospace;">PersistenceManager.refresh()</span>.
This method takes one object as argument, and returns the same object. <br>
Thanks to Java's generics you do not need to manually cast the return
value. </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>List<font color="#990000">&lt;</font>TextObject<font
color="#990000">&gt;</font>objects <font color="#990000">=</font> persistTwo<font
color="#990000">.</font><b><font color="#000000">getObjects</font></b><font
color="#990000">(</font>TextObject<font color="#990000">.</font><b><font
color="#0000ff">class</font></b><font color="#990000">,</font> <b><font
color="#0000ff">new</font></b> <b><font color="#000000">All</font></b><font
color="#990000">());</font><br><font color="#008080">TextObject</font> copy <font
color="#990000">=</font> simpleObjects<font color="#990000">.</font><b><font
color="#000000">get</font></b><font color="#990000">(</font><font
color="#993399">0</font><font color="#990000">);</font><br><br><i><font
color="#9a1900">//...lots of operations</font></i><br><br><i><font
color="#9a1900">//get the latest version from the database</font></i><br><font
color="#008080">TextObject</font> copy2 <font color="#990000">=</font> persistTwo<font
color="#990000">.</font><b><font color="#000000">refresh</font></b><font
color="#990000">(</font>copy<font color="#990000">);</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
Note that after this both <span style="font-family: monospace;">copy</span>
and <span style="font-family: monospace;">copy2</span> will reference
the same object. If you do not want to create a new reference, simply
do this <span style="font-family: monospace;">:</span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>copy <font color="#990000">=</font> persistTwo<font
color="#990000">.</font><b><font color="#000000">refresh</font></b><font
color="#990000">(</font>copy<font color="#990000">);</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<span> <span> <br>
If the object you are refreshing have been deleted, the return value
will equal <span style="font-family: monospace;">null</span>. </span>
</span> <span> </span> </div>
<h3 style="margin-left: 40px;"> <a id="droptables">Drop tables:</a>
</h3>
<div style="margin-left: 40px;">
<table summary="" class="warningtable">
<tbody>
<tr>
<td style="vertical-align: top;"> <img
style="width: 48px; height: 48px;" alt="warning" title="warning"
src="img/warning.png"> </td>
<td style="vertical-align: middle;">If
you drop the tables for a class, all objects of that class will be
deleted. All objects of its subclasses will be deleted. All objects of
all classes that contain references to the deleted class will be
deleted.</td>
</tr>
</tbody>
</table>
<br>
The <span style="font-family: monospace;">PersistenceManager.dropTable(Class
c)</span> method takes a <span style="font-family: monospace;">Class </span>as
its
only argument. When the method completes successfully the tables
that make up the given class and all tables that depend on it will have
been deleted. This method is extremely powerful and should be used with
great care. </div>
<h3 style="margin-left: 40px;"> <a id="duplicate">Duplicate
database: </a> </h3>
<div style="margin-left: 40px;"> <span> <span
style="font-family: monospace;">PersistenceManager</span> offers a
convenience method that duplicates a database, <span
style="font-family: monospace;">PersistenceManager.duplicate(PersistenceManager
target).</span> This method is called on the source <span
style="font-family: monospace;">PersistenceManager</span>, and takes a
target <span style="font-family: monospace;">PersistenceManager</span>
as its only argument. When the method call completes, the target will
contain an exact copy of the source. </span> </div>
<h3 style="margin-left: 40px;"> <a id="ignored">Ignored object
properties: </a> </h3>
<div style="margin-left: 40px;"> <span> Sometimes you do not
want Conserve to store a particular property of an object. This can be
achieved by using the <span style="font-family: monospace;">Transient </span>annotation
on the accessor of the property you wish to ignore, like this: </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>@Transient<br><b><font color="#0000ff">public</font></b> <font
color="#009900">int</font> <b><font color="#000000">getSomeValue</font></b><font
color="#990000">()</font><br><font color="#ff0000">{</font><br> <b><font
color="#0000ff">return</font></b> <font color="#993399">42</font><font
color="#990000">;</font><br><font color="#ff0000">}</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<span> <br>
This tells conserve to ignore the <span style="font-family: monospace;">getSomeValue</span>
accessor and treat it as if it does not exist. This is useful if your
class has a property that does is derived from other properties, or if
using a property's mutatorwould overwrite another property. <br>
</span> </div>
<h3 style="margin-left: 40px;"> <a id="customtablenames">Custom
tablenames:</a> </h3>
<div style="margin-left: 40px;"> <span>Conserve generates unique
table names </span>from the canonical class name, but if you wish you
can override this name by using the <span
style="font-family: monospace;">TableName</span> annotation. The <span
style="font-family: monospace;">TableName</span> annotation is applied
to the class, like this: <br>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>@<b><font color="#000000">TableName</font></b><font
color="#990000">(</font><font color="#ff0000">"MyTable"</font><font
color="#990000">)</font><br><b><font color="#0000ff">public</font></b> <b><font
color="#0000ff">class</font></b> <font color="#008080">BarTextObject</font> extends TextObject<br><font
color="#ff0000">{</font><br> <i><font color="#9a1900">//...</font></i><br><font
color="#ff0000">}</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
Note that whether table name case is preserved and respected is up to
the underlying RDBMS, but in general it is safe to assume that case
will <span style="font-style: italic;">not</span> be preserved. <br>
</div>
<h3 style="margin-left: 40px;"> <a id="customcolumnnames">Custom
column names:</a> </h3>
<div style="margin-left: 40px;"> <span>Conserve generates unique
column names </span>from the accessor method name, but if you wish you
can override this name by using the <span
style="font-family: monospace;">ColumnName</span> annotation. The <span
style="font-family: monospace;">ColumnName</span> annotation is
applied to the accessor method, like this: <br>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>@<b><font color="#000000">ColumnName</font></b><font
color="#990000">(</font><font color="#ff0000">"MyTable"</font><font
color="#990000">)</font><br><b><font color="#0000ff">public</font></b> int </tt><span><span><span><tt><b><font
color="#000000">getFoo</font></b><font color="#990000">()</font></tt></span></span></span><span><span><span><br><tt><font
color="#ff0000">{</font><br> <i><font color="#9a1900">//...</font></i><br><font
color="#ff0000">}</font></tt></span></span></span></pre>
</td>
</tr>
</tbody>
</table>
<br>
Note that whether column name case is preserved and respected is up to
the underlying RDBMS, but in general it is safe to assume that case
will <span style="font-style: italic;">not</span> be preserved. <br>
</div>
<h3 style="margin-left: 40px;"> <a id="customstringlength">Custom
String length: </a> </h3>
<div style="margin-left: 40px;"> <span>Some databases have
limits on the length of String entries. In these
cases Conserve selects the largest legal size. If this is not what you
desire for practical orperformance reasons, use the <span
style="font-family: monospace;">MaxLength</span> annotation on the
appropriate accessor method, like this: </span> <br>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>@<span
style="font-family: monospace; font-weight: bold;">MaxLength</span><font
color="#990000">(</font></tt><tt><font color="#993399">1024</font></tt><span><font
color="#990000">)</font></span><br><span><span><span><tt><b><font
color="#0000ff">public</font></b> String <b><font color="#000000">getFoo</font></b><font
color="#990000">()</font><br><font color="#ff0000">{</font><br> <b><font
color="#0000ff">return</font></b> </tt><tt><font color="#ff0000">"Bar"</font></tt><span><span><tt><font
color="#990000">;</font><br><font color="#ff0000">}</font></tt></span></span></span></span></span></pre>
</td>
</tr>
</tbody>
</table>
<br>
</div>
<h3 style="margin-left: 40px;"> <a id="usingclobsandblobs">Using
CLOBs and BLOBs: </a> </h3>
<div style="margin-left: 40px;"> <span> Conserve will store byte
and character arrays as arrays, but you can choose to have them stored
as BLOBs and CLOBs, respectively. <br>
If the underlying RDBMS does not support CLOBs and/or BLOBs Conserve
will emulate the functionality seamlessly. <br>
<br>
Just apply the <span style="font-family: monospace;">AsClob</span> or <span
style="font-family: monospace;">AsBlob</span> annotations to an
accessor that returns a character array or byte array, respectively: </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>@<b>AsClob<br><font color="#0000ff">public</font></b> <font
color="#009900">char</font><font color="#990000">[]</font> <b><font
color="#000000">getBar</font></b><font color="#990000">()</font><br><font
color="#ff0000">{</font><br> <b><font color="#0000ff">return</font></b> <b><font
color="#0000ff">new</font></b> <font color="#009900">char</font><font
color="#990000">[]</font><font color="#ff0000">{</font><font
color="#ff0000">'4'</font><font color="#990000">,</font><font
color="#ff0000">'2'</font><font color="#ff0000">}</font><font
color="#990000">;</font><br><font color="#ff0000">}</font><br><br>@<b>AsBlob<br><font
color="#0000ff">public</font></b> byte<font color="#990000">[]</font> <b><font
color="#000000">getBaz</font></b><font color="#990000">()</font><br><font
color="#ff0000">{</font><br> <b><font color="#0000ff">return</font></b> <b><font
color="#0000ff">new</font></b> byte<font color="#990000">[]</font><font
color="#ff0000">{</font><font color="#993399">42</font><font
color="#ff0000">}</font><font color="#990000">;</font><br><font
color="#ff0000">}</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<br>
These annotations are ignored if they are applied to methods that do
not return the correct type of array. <br>
</div>
<h3 style="margin-left: 40px;"> <a id="indexes">Indices:</a> </h3>
<div style="margin-left: 40px;"> <span>Indices are useful for
speeding up searches, which is why Conserve allows you to add indices
on properties. </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>@<b><font color="#000000">Indexed</font></b><font
color="#990000">(</font><font color="#ff0000">"MyIndex"</font><font
color="#990000">)</font><br><b><font color="#0000ff">public</font></b> <font
color="#009900">int</font> <b><font color="#000000">getFoo</font></b><font
color="#990000">()</font><br><font color="#ff0000">{</font><br> <b><font
color="#0000ff">return</font></b> <font color="#993399">42</font><font
color="#990000">;</font><br><font color="#ff0000">}</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<span> <br>
If you want to add more than one column to the same index, just use the
same index name: </span>
<table summary="" style="text-align: left; width: 100%;"
class="newcolor" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="vertical-align: top;">
<pre><tt>@<b><font color="#000000">Indexed</font></b><font
color="#990000">(</font><font color="#ff0000">"MyIndex"</font><font
color="#990000">)</font><br><b><font color="#0000ff">public</font></b> <font
color="#009900">int</font> <b><font color="#000000">getFoo</font></b><font
color="#990000">()</font><br><font color="#ff0000">{</font><br> <b><font
color="#0000ff">return</font></b> <font color="#993399">42</font><font
color="#990000">;</font><br><font color="#ff0000">}</font><br><br>@<b><font
color="#000000">Indexed</font></b><font color="#990000">(</font><font
color="#ff0000">"MyIndex"</font><font color="#990000">)</font><br><b><font
color="#0000ff">public</font></b> <font color="#009900">long</font> <b><font
color="#000000">getTime</font></b><font color="#990000">()</font><br><font
color="#ff0000">{</font><br> <b><font color="#0000ff">return</font></b> System<font
color="#990000">.</font><b><font color="#000000">currentTimeMillis</font></b><font
color="#990000">();</font><br><font color="#ff0000">}</font></tt></pre>
</td>
</tr>
</tbody>
</table>
<span> <span> <br>
Please note that some RDBMSs will truncate strings that are used in
indices. </span> </span> </div>
</td>
</tr>
</tbody>
</table>
<hr style="width: 100%; height: 2px; margin-left: 0px;">
<table summary="" style="text-align: left; width: 100%;" border="0"
cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="vertical-align: top;"> <small> <small> Last updated:
2012-01-02<br>
<br>
Hosted by: <br>
<a href="http://sourceforge.net/projects/conserve"> <img
src="http://sflogo.sourceforge.net/sflogo.php?group_id=268666&amp;type=10"
alt="Get Conserve at SourceForge.net. Fast, secure and Free Open Source software downloads"
style="border: 0px solid ; width: 80px; height: 15px;"> </a> </small>
</small> </td>
<td style="vertical-align: top; text-align: right;"> <a
href="http://sourceforge.net/donate/index.php?group_id=268666"> <img
src="http://images.sourceforge.net/images/project-support.jpg"
alt="Support This Project"
style="border: 0px solid ; width: 88px; height: 32px;"> </a> </td>
</tr>
</tbody>
</table>
</body>
</html>