orm

Usage of “aliased” in SQLAlchemy ORM

荒凉一梦 提交于 2020-05-10 07:36:08
问题 From the SQLAlchemy ORM Tutorial: You can control the names using the label() construct for scalar attributes and aliased for class constructs: >>> from sqlalchemy.orm import aliased >>> user_alias = aliased(User, name='user_alias') >>> for row in session.query(user_alias, user_alias.name.label('name_label')).all(): ... print row.user_alias, row.name_label This seems to be a lot more typing and a lot less readable than the plain class-instrumented descriptors: >>> for row in session.query

Django基于ORM操作数据库的方法详解

安稳与你 提交于 2020-05-08 04:49:53
本文实例讲述了Django基于ORM操作数据库的方法。分享给大家供大家参考,具体如下: 1、配置数据库 vim settings #HelloWorld/HelloWorld目录下 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', #mysql数据库中第一个库test 'NAME': 'test', 'USER': 'root', 'PASSWORD': '123456', 'HOST':'127.0.0.1', 'PORT':'3306', }, 'article': { 'ENGINE': 'django.db.backends.mysql', # mysql数据库中第二个库test2 'NAME': 'test2', 'USER': 'root', 'PASSWORD': '123456', 'HOST':'127.0.0.1', 'PORT':'3306', } } 2、在项目目录中建立“web站点”(app) django-admin.py startapp blog ##HelloWorld/目录下建立网站app,我建了两个app(blog和article) 3、配置新建的app(blog和article) vim settings ##/HelloWorld/HelloWorld目录下

SOD让你的旧代码焕发青春

风格不统一 提交于 2020-05-05 13:54:25
最近接手了一个旧的系统,各种陈旧的问题比较多,其中最棘手的就是操作数据库的部分,具体如下:    1、核心库是一个最后修改时间为2008年的库,先不说有多陈旧,现在这个库只是一个DLL文件,没有源码,也已经没人知道里面都实现了些啥功能,就算你怀疑数据库读写有问题,也无法验证和调试,反编译出来的源码也没法用。   2、这个库用的是System.Data.OracleClient操作Oracle,问题很明显,依赖于Oracle客户端,区分32位和64位,一旦客户的Oracle客户端出点问题,系统就歇菜了,而且部署起来也不方便。   3、操作数据库用的全是拼写SQL语句,对我这种习惯了ORM的人,实在太痛苦了,而且对JSON和流数据的支持也不是很好,这两种数据都需要单独处理,无形中增加了操作数据库的次数。   明确了问题,也就知道了最终想要达到的效果,具体如下:    1、有源码,风险可控。   2、不依赖于Oracle客户端,不区分32位和64位,这个其实Oracle官方已经提供了解决方案,就是ODP.NET,最新版本已经快可以达到前面两个要求,而且提供了NUGET包,引用简单。   3、既要兼容旧有的DbHelper的操作方式,也要支持ORM的操作方式,一是因为原来代码量过大,不可能快速完全转到ORM,所以必须兼容旧有的操作方式,同时,ORM也是必须的,毕竟自己用着顺手。  

Hibernate relation with aggregation

♀尐吖头ヾ 提交于 2020-05-01 02:49:00
问题 @Entity public class Device { private long id; private String deviceName; //column D_NAME private Set<LoginDate> loginDates; } /** Audit table being filled by some other processes on the network */ @Entity public class LoginDate { private long id; //pk private String deviceName; //column D_NAME private Date loginDate; } My aim to display the last login date of the device on the report screens; I have implemented OneToMany relation between Device and LoginDate tables where it is using OrderBy

Hibernate relation with aggregation

删除回忆录丶 提交于 2020-05-01 02:48:24
问题 @Entity public class Device { private long id; private String deviceName; //column D_NAME private Set<LoginDate> loginDates; } /** Audit table being filled by some other processes on the network */ @Entity public class LoginDate { private long id; //pk private String deviceName; //column D_NAME private Date loginDate; } My aim to display the last login date of the device on the report screens; I have implemented OneToMany relation between Device and LoginDate tables where it is using OrderBy

How to map calculated properties with JPA and Hibernate

时光怂恿深爱的人放手 提交于 2020-04-30 06:38:14
问题 My Java bean has a childCount property. This property is not mapped to a database column . Instead, it should be calculated by the database with a COUNT() function operating on the join of my Java bean and its children. It would be even better if this property could be calculated on demand / "lazily", but this is not mandatory. In the worst case scenario, I can set this bean's property with HQL or the Criteria API, but I would prefer not to. The Hibernate @Formula annotation may help, but I

Hibernate complains for null @Id in @OneToOne even if it is not null

萝らか妹 提交于 2020-04-18 06:55:19
问题 I am trying to achieve @OneToOne association, using the same @Id between a Car and a Person . A Person is has an optional Car , but a Car has a required Person (hence I need a foreign key inside "cars" table pointing an existing Person ): @Entity @Table(name = "persons") public class Person { @Id @Column(name = "name") private String name; @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "person") private Car car; /** * For hibernate */ @SuppressWarnings("unused")

Parent-child relation between two objects causes JSON StackOverflowError

拥有回忆 提交于 2020-04-16 12:55:08
问题 I am trying to achieve a parent-child relation between some objects and I ran into a bit of trouble. In my case, I am trying to store objects within other objects (e.g. container stores multiple items or other containers with items). The tricky part is that every object in the storage should be able to tell what it's outermost parent object is. While this seems to work in my in-memory database (using h2 at the moment), trying to get a JSON representation of all my storage items gives this (I

【MyBatis框架】mapper配置文件-关于动态sql

故事扮演 提交于 2020-04-11 13:26:21
动态sql 1.什么是动态sql mybatis核心 对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接、组装。 2.需求 用户信息综合查询列表和用户信息查询列表总数这两个statement的定义使用动态sql。 对查询条件进行判断,如果输入参数不为空才进行查询条件拼接。 3.mapper.xml 原查询语句配置: <mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper"> <!-- 用户信息综合查询 #{UserCustom.sex}取出包装对象中性别值 ${UserCustom.username}取得pojo包装对象中用户名称 --> <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo" resultType="cn.edu.hpu.mybatis.PO.UserCustom"> select * from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%' </select> <!-- 用户信息综合查询总数 --> <select id="findUserCount"

android原生的数据库实现[ContentProvider+SQLiteOpenHelpe...

人盡茶涼 提交于 2020-04-10 08:55:57
先吐槽下:最近几周压力老大了,前面我们三个ios开发人员花了一个多月开发的应用要移植到android上,由于应用相对比较复杂,有拖拽排序、离线下载、二维码扫描,而这次,另一个ios开发人员离职,剩下的另一个还没做个android,得由我带,于是,我估计了下开发时间,大约34天,我将计划发给领导,领导却说最多给我15天,我半天没说一个字:*&%¥#%**&⋯⋯&& 哎,出来混的,不管怎样,尽量吧。 这个开发,我们四张表要操作,相对于一般的移动应用,还是比较大的,故数据库实现肯定是整个开发的第一个和至关重要的一步,于是,我整理了下面这个demo。 其实网上有很多比较不错的orm移动开发框架,相对还是比较优秀,可以节省大把的开发时间,但我个人认为,现在这个还不是很稳定,而且,在效率上,我持保守态度,移动开发上, 应相对的少用反射和抽象接口 ,而网络上的一些第三方的框架,却抓住这个大用特用,个人觉得,不大好,另外,采用android原生的数据库实现方案,还有个极大的好处是: 很容易实现产品生态系统,这个是其他的orm所不易达到的 。也即,可以轻而易举地实现我们的几个应用间的数据共享和交互,URI访问即可,从而形成一个生态圈的目的,具体的,就不多说了。 这个demo是一个完整的db操作,但只给出了一个实体的db实现,不过,足以: 程序结构图: 后面的所有db操作