【问题标题】:How to code query from SQL in Java for object oriented metamodel如何在 Java 中为面向对象的元模型编写 SQL 查询代码
【发布时间】:2016-02-14 14:26:40
【问题描述】:

我正在尝试学习如何使用名为 Eyedb(在 linux 上)的工具来创建面向对象的模型数据库。我已经为这个数据库制作了一些类和记录生成器,但我不知道如何使用 Java 编写查询代码。

数据库的结构如下:

    class Lecturer {

        attribute string<32> name;
        attribute strin<32> surname;
        relationship set <Lecture*> lectures inverse Lecture::Lecturer;
};

class Item{
        attribute string<32> name;
        attribute string<512> description;
};

class Topic extends Item {
        relationship set<Lecture*> tlecture inverse Lecture::Topic;
};


class Lecture {
        relationship Lecturer *llecturer inverse Lecturer::lectures;
        relationship Topic *ltopic inverse Topic::tlecture;
        relationship set <Content*> lcontent inverse Content::Lecture;
        relationship set <Equipment*> lequipment inverse Content::Lecture;
        relationship Room_timeslot* lroom_timeslot inverse Room_timeslot::rlecture;                                                     
};

class Content extends Item {

        attribute int level;
        relationship set <Content*> subcontent inverse Content::supcontent; 
        relationship set <Content*> supcontent inverse  Content::subcontent;    
        relationship set <Teachingmaterial*> cteachingmaterial inverse Teachingmaterial::Content;
};

class Teachingmaterial extends Item {

        attribute string link;
        relationship set <Content*> tcontent inverse Content::Teachingmaterial;
};

class Equipment extends Item {
        attribute int quantity;
        attribute string symbol;
        relationship set<Lecture*> electure inverse Lecture::Equipment;
        relationship Room *eroom inverse Room::requipment;

};

class Room {
        relationship set <Equipment*> requipment inverse Equipment::Room
        attribute string name;
        attribute int number;
        attribute string symbol;
        attribute string building;
        attribute int floor;
        attribute string wing;
        relationship set <Room_timeslot*> rroom_timeslot inverse Room_timeslot::Room;
};

class Room_timeslot {
        relationship Room *room inverse room::rroom_timeslot;
        relationship Lecture* rlecture inverse Lecture::Room_timeslot;
        relationship set <Timeslot*> rtimeslot inverse Timeslot::lroom_timeslot;
};

class Timeslot {
        attribute string name;
        attribute int number;
        attribute time timemargin_start;
        attribute time timemargin_end;
        relationship set <Room_timeslot*> troom_timeslot inverse Room_timeslot::rtimeslot;
};

我想进行如下所示的查询:

SELECT lr.name as lecturer, t.name as topic FROM topic t        
JOIN lecture l ON t.id_topic = l.id_topic
JOIN lecturer lr ON lr.id_lecturer = l.id_lecturer
ORDER BY lr.name

select t.name as topic from topic t 
join lecture l on t.id_topic = l.id_topic 
join lecturer lr on lr.id_lecturer = l.id_lecturer
where lr.name = "name1"

SELECT tm.name as teaching_material, e.name as equipment, lr.name as lecturer FROM teachingmaterial tm
JOIN teaching_content tc ON tc.id_teachingmaterial = tm.id_teachingmaterial
JOIN content c ON c.id_content = tc.id_content
JOIN content_lecture cl ON cl.id_content = c.id_content
JOIN lecture l ON l.id_lecture = cl.id_lecture
JOIN lecture_equipment le ON le.id_lecture = l.id_lecture
JOIN equipment e ON e.id_equipment = le.id_equipment
JOIN lecturer lr ON lr.id_lecturer = l.id_lecturer

【问题讨论】:

  • 这不是学习学校或教学中心。在这里您可以获得技术问题的帮助。

标签: java sql object-oriented-database


【解决方案1】:

Java 使用 JDBC 与关系数据库进行通信。如果您的数据库有 JDBC 驱动程序,那么一切就绪。如果没有,那你就不走运了。 SQL Server 和 MySQL 都有 JDBC 驱动程序,所以它们应该不是问题。你需要这些和JDBC tutorial

看起来EyeDB 是一个我从未听说过的对象数据库。它声称可以被 C++ 和 Java 使用。

我希望至少有一个“hello world”连接示例。我怀疑任何甚至无法正确获取其网页上的 HTML 的软件。

Java documentation 可用。看起来很糟糕。这是一个要求吗?有更好的数据库,包括关系(MySQL、SQLLite 或 PostgreSQL)、对象(例如JODB)或图形(NEO4J)。一定要用这个吗?

EyeDB 文档清楚地说明了这些步骤。 sample problem

  1. 使用 EyeDB 对象定义语言 (ODL) 定义架构
  2. 使用您下载的 EyeDB eyedbodl 工具从架构中生成 Java 类。
  3. 使用生成的与数据库交互的类编写客户端程序。

一切都在那里。试试看吧。

【讨论】:

  • 遗憾的是这是要求。我必须比较关系数据库(其中 2 个,一个在 sql server,一个在 phpmyadmin)和一个面向对象。我会使用另一个(不是 eyedb),但我上面的某个人认为这个是最接近面向对象的“学说”(对不起我的英语)。我没有在文档中找到与查询相关的任何内容,所以我在这里问。我看到了插入、删除和修改(更新)的样子,但我不知道如何将我的查询从 sql 调整为 java。
  • 您的对象数据库查询不会是 SQL,因为那是关系数据库的语言。对象数据库不是关系型的。您可以使用 JDBC 在 Java 中轻松地与 SQL Server 进行通信。看看 JDBC 教程。 EyeDB 是您自己的事。 docs.oracle.com/javase/tutorial/jdbc
  • 我听说我应该在那些查询中使用 OQL,但我不知道 if 的语法。我应该搜索它还是使用您描述的工具?我很抱歉我缺乏知识,但我是这部分 IT 的新手,我没有多少时间来完成我的任务。
  • 没有面向对象的查询语法。它们都是非标准的。这就是为什么它们是利基产品而不是普遍使用的原因之一。您应该阅读 EyeDB 上的文档链接并了解如何使用它。你没时间不是我的问题;我不能给你你需要学习它的时间。试一试,尽力而为。
  • 对于我的课程,我想插入看起来像这样:'讲师讲师 = 新讲师(新); //数据库名称?讲师.setname("姓名"+Integer.toString(i));讲师.setsurname("姓_"+Integer.toString(i));讲师.store();'但是如何做与我写得更高的相同查询呢?
猜你喜欢
  • 1970-01-01
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
  • 2018-02-20
  • 2017-11-18
  • 2017-12-28
  • 1970-01-01
相关资源
最近更新 更多