初识DB4O

    DB4O?
    新出的OODBMS~取谐音DB for O 。一个优秀的OODBMS解决方案,它出现的定位就是为了开发嵌入式和小型应用,同HSQLDB的定位相仿。不过DB4O的的优点则是,本身是OODBMS,那么对它的操作自然就是基于OO的,同Hiberante一样,但是Hiberante是ORM解决方案,而DB4O则是原生OODBMS方案。看看下面的例子

一个同Hibernate相同的POJO,没有任何方法,也没有跟DB4O有任何耦合。
 1 None.gif package  name.lgn.db4o.test.model;
 2 None.gif
 3 ExpandedBlockStart.gifContractedBlock.gif public   class  Person  dot.gif {
 4InBlock.gif
 5InBlock.gif    private String firstName;
 6InBlock.gif
 7InBlock.gif    private String lastName;
 8InBlock.gif
 9InBlock.gif    private int age;
10InBlock.gif
11ExpandedSubBlockStart.gifContractedSubBlock.gif    public Person(String firstName, String lastName, int age) dot.gif{
12InBlock.gif        this.firstName = firstName;
13InBlock.gif        this.lastName = lastName;
14InBlock.gif        this.age = age;
15ExpandedSubBlockEnd.gif    }

16InBlock.gif
17ExpandedSubBlockStart.gifContractedSubBlock.gif    public String getFirstName() dot.gif{
18InBlock.gif        return firstName;
19ExpandedSubBlockEnd.gif    }

20InBlock.gif
21ExpandedSubBlockStart.gifContractedSubBlock.gif    public void setFirstName(String value) dot.gif{
22InBlock.gif        firstName = value;
23ExpandedSubBlockEnd.gif    }

24InBlock.gif
25ExpandedSubBlockStart.gifContractedSubBlock.gif    public String getLastName() dot.gif{
26InBlock.gif        return lastName;
27ExpandedSubBlockEnd.gif    }

28InBlock.gif
29ExpandedSubBlockStart.gifContractedSubBlock.gif    public void setLastName(String value) dot.gif{
30InBlock.gif        lastName = value;
31ExpandedSubBlockEnd.gif    }

32InBlock.gif
33ExpandedSubBlockStart.gifContractedSubBlock.gif    public int getAge() dot.gif{
34InBlock.gif        return age;
35ExpandedSubBlockEnd.gif    }

36InBlock.gif
37ExpandedSubBlockStart.gifContractedSubBlock.gif    public void setAge(int value) dot.gif{
38InBlock.gif        age = value;
39ExpandedSubBlockEnd.gif    }

40InBlock.gif
41ExpandedBlockEnd.gif}

42 None.gif

在看,DB4O的解决方案:
 1 None.gif package  name.lgn.db4o.test;
 2 None.gif
 3 None.gif import  name.lgn.db4o.test.model.Person;
 4 None.gif
 5 None.gif import  org.junit.Test;
 6 None.gif
 7 None.gif import  com.db4o.Db4o;
 8 None.gif import  com.db4o.ObjectContainer;
 9 None.gif import  com.db4o.ObjectSet;
10 None.gif
11 ExpandedBlockStart.gifContractedBlock.gif public   class  TestDB4O  dot.gif {
12InBlock.gif
13InBlock.gif    @Test
14ExpandedSubBlockStart.gifContractedSubBlock.gif    public void testDB() dot.gif{
15InBlock.gif        ObjectContainer db = null;
16ExpandedSubBlockStart.gifContractedSubBlock.gif        try dot.gif{
17InBlock.gif            db = Db4o.openFile("persons.data");
18InBlock.gif
19InBlock.gif            Person brian = new Person("Brian""Goetz"39);
20InBlock.gif            Person jason = new Person("Jason""Hunter"35);
21InBlock.gif            Person clinton = new Person("Brian""Sletten"38);
22InBlock.gif            Person david = new Person("David""Geary"55);
23InBlock.gif            Person glenn = new Person("Glenn""Vanderberg"40);
24InBlock.gif            Person neal = new Person("Neal""Ford"39);
25InBlock.gif
26InBlock.gif            db.set(brian);
27InBlock.gif            db.set(jason);
28InBlock.gif            db.set(clinton);
29InBlock.gif            db.set(david);
30InBlock.gif            db.set(glenn);
31InBlock.gif            db.set(neal);
32InBlock.gif
33InBlock.gif            db.commit();
34InBlock.gif
35InBlock.gif            // Find all the Brians
36InBlock.gif            ObjectSet brians = db.get(new Person("Brian"null0));
37InBlock.gif            while (brians.hasNext())
38InBlock.gif                System.out.println(brians.next());
39InBlock.gif
40ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) dot.gif{
41InBlock.gif            e.printStackTrace();
42ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 finally dot.gif{
43InBlock.gif            if (db != null)
44InBlock.gif                db.close();
45ExpandedSubBlockEnd.gif        }

46InBlock.gif
47ExpandedSubBlockEnd.gif    }

48ExpandedBlockEnd.gif}

49 None.gif
存入数据用db.set(...);取出数据用db.get(...),是不是非常亲切和简单?
    对于不需要使用大型DB,但又需要存储数据的时候不需要再用hibernate+hsqldb了,只需要使用DB4O,就全带了,又有简单的数据库操作,又有数据的存储,还有数据完整性的保证,简单方便,何乐不为呢?RCP开源项目著名的RSSOWL在2.0中也已经把原有的hiberante+hsqldb替换为DB4O的解决方案了。
    虽然OODBMS的路还有很长,而且就现在看,和RDBMS还有很长的距离,但是从DB4O上,我们还是看到了希望。在DB4O的眼中Hibernate,jdo等等都是暂时的解决方案,对于OODBMS来说ORM本来就是不存在的东西,ORM仅仅是为了解决RDBMS到OO的映射,而OODBMS本来就是原生的,免去了中间层,免去了烦恼。

下载 db4o:这是一种用于 Java 的开源对象数据库。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值