电信机顶盒灯一蓝一红一直蓝灯电视上就显示一个中国移动怎么也看不了电视

2311人阅读
Rails(218)
1、1对1关系的实现& book与user
&&&&& belongs_to:user
&&&&&& has_one :book&&&&&&&& #体现两者之间的1对1关系
& def self.up
&&& create_table :books do |t|
&&&&& t.integer :&&&&&#存储 user的 id,
&&&&& t.string :title
&&&&& t.string :author
&&&&& t.timestamps
& def self.up
&&& create_table :users do |t|
&&&&& t.string :name
&&&&& t.timestamps
u= User.new
b=Book.last
此时,b.user_id = 2
2、 单表继承&&&admin单表继承user
classAdmin & User
&&& p &i am an admin&
classUser & ActiveRecord::Base
&&& p &i am an user&
db/migrate层
admin.rb是不需要的
&&& create_table :users do |t|
&&&&& t.string :name
&&&&& t.string :type&&&&&&&&&&&&&&&&&&& #type为保留字,用于单表继承
&&&&& t.timestamps
&a = Admin.new
& #&Admin id: nil, name: nil, type:&Admin&, created_at: nil, updated_at: nil&
#a的type字段为&Admin&,此条记录仍保存在User表中
3、 1对多关系 book与department
&&&&&& &belongs_to:department
department.rb
& def self.up
&&& create_table :books do |t|
&&&&& t.integer :department_id
&&&&& t.string :title
&&&&& t.string :author
&&&&& t.timestamps
department.rb
& def self.up
&&& create_table :departments do |t|
&&&&& t.string :name
&&&&& t.timestamps
.name=”销售”
b.title = “zh”
d.books &&b
此时,b.department_id= d.id
4、多对多的关系& 与
& has_many :classifies, :through =&:manifests, :source =& :classify
& has_many :s
& has_many :books, :through =& :manifests,:source =& :book
多对多的关系在第三个文件中体现&& manifest.rb
& belongs_to :book
& belongs_to :classify
create_table:books do |t|
&&&&& t.string :title
&&&&& t.string :author
&&&&& t.timestamps
classify.rb
&&& create_table :classifies do |t|
&&&&& t.string :kind
&&&&& t.timestamps
manifest.rb&&&&&&&&&&& #此文件中需要存储有多对多关系对象的id
&&& create_table :manifests do |t|
&&&&& t.integer :book_id
&&&&& t.integer :classify_id
c= Classify.new
&c.kind = &reading&
c= Classify.new
&c.kind = &science&
id&&&&&&&&&& title
1&&&&&&&&&&&& ch
2& &&&&&&&& &&&&&&en
3&&&&&&&&&&& &&&&&jap
&b = Book.first
&c& =Classify.last
&c.books && b&&&& #建立book与 Classify之间的关系
manifest表为:
id&& book_id&&&Classify_id
1&&&&&&& 1&&&&&&&&&&&&&&&&& 2
b= Book.last
b.classifies& && c
manifest表为:
id&& book_id&&&Classify_id
1&&&&&&& 1&&&&&&&&&&&&&& &&&2
2&&& &&&&&3&&&&&&&&&&&&&&&&& && 2
5、多态&&& 、user、department
department.rb
has_many:books,:as =& :borrowed, && # dependent的含义是如果book消失,则department所对应的对象消失
&&& p &i am an department&
has_many:books,:as =& :borrowed, :dependent =& :destroy
&&& p &i am an user&
db/migrate层
& create_table :books do |t|
&&&&& t.integer :category_id
&&&&& t.integer :_id
&&&&& t.string :title
&&&&& t.string :author
&&&&& t.string :borrowed_type
&&&&& t.integer :borrowed_id
&&&&& t.integer :department_id
&&&&& t.timestamps
u= User.first
u.im&&&&&&&&& #&i am an user&
d.im&&&&&&& #&i am an department&
u. books =& Book.last&&&&&& #此时book表中Book.last& 所对应对象的borrowed_type更新为user,borrowed_id更新为u. id
d.books = Book.first&&&&&& #此时book表中Book.first 所对应对象的borrowed_type更新为department,borrowed_id更新为d. id
6、named_scope(用类名来调用)
& named_scope&:name,:conditions =&
语句”, order=&“SQL 语句”,& group=&”字段名”
& named_scope :unborrowed ,:conditions =&&borrowed_id is null&,:order =&& desc&
& named_scope :all_borrowed_type ,:group =&&borrowed_type&
Book.unborrowed&&& #查出 borrowed_id字段为 null的对象,并按 title的降序排列
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1124114次
积分:12128
积分:12128
排名:第855名
原创:66篇
转载:949篇
译文:11篇
评论:17条
(1)(5)(9)(2)(8)(7)(7)(14)(15)(1)(6)(12)(16)(3)(13)(24)(7)(4)(62)(49)(17)(15)(34)(47)(28)(19)(26)(95)(36)(30)(52)(36)(65)(4)(23)(13)(80)(17)(21)(31)(17)(3)(16)(35)(3)请问model中的关联表怎么用? · Ruby China
看了官方的教程,设置能用好,但在读取数据时如何操作?比如读取中主表,如何把关联的子表中的多条数据一起读出来?
官方教程大多讲的是如何定义
Agile Web Development with Rails,4th Edition 建议看本书吧,理解的更快。
比如说A has_many B ,而B belongs_to A
那么可以直接
a.b.each do |xxx|
这样来遍历。
一对一,一对多,多对多,多态等…
后方可回复, 如果你还没有账号请点击这里 。
共收到 3 条回复hibernate之多态关联(多态的一对多,利用unionsubclass)
hibernate之多态关联(多态的一对多,利用union-subclass)
下面以关联映射中映射的持久化类中声明了&union-subclass&为例:
&project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&
&modelVersion&4.0.0&/modelVersion&
&groupId&hibernateTest&/groupId&
&artifactId&hibernateTest&/artifactId&
&version&1.0-SNAPSHOT&/version&
&packaging&jar&/packaging&
&name&hibernateTest&/name&
&url&http://maven.apache.org&/url&
&dependencies&
&dependency&
&groupId&junit&/groupId&
&artifactId&junit&/artifactId&
&version&3.8.1&/version&
&scope&test&/scope&
&/dependency&
&dependency&
&groupId&org.hibernate&/groupId&
&artifactId&hibernate-core&/artifactId&
&version&3.3.1.GA&/version&
&/dependency&
&dependency&
&groupId&org.slf4j&/groupId&
&artifactId&slf4j-nop&/artifactId&
&version&1.5.2&/version&
&/dependency&
&dependency&
&groupId&javassist&/groupId&
&artifactId&javassist&/artifactId&
&version&3.4.GA&/version&
&/dependency&
&dependency&
&groupId&c3p0&/groupId&
&artifactId&c3p0&/artifactId&
&version&0.9.1&/version&
&/dependency&
&dependency&
&groupId&com.oracle&/groupId&
&artifactId&ojdbc14&/artifactId&
&version&10.2.0.3.0&/version&
&scope&runtime&/scope&
&/dependency&
&/dependencies&
&finalName&hibernateTest&/finalName&
&resources&
&resource&
&directory&src/main/resources&/directory&
&/resource&
&resource&
&directory&src/main/java&/directory&
&excludes&
&exclude&/*.java&/exclude&
&/excludes&
&/resource&
&/resources&
&artifactId&maven-compiler-plugin&/artifactId&
&configuration&
&source&1.6&/source&
&target&1.6&/target&
&encoding&UTF-8&/encoding&
&/configuration&
&/plugins&
&/project&
resources/hibernate.cfg.xml:
&?xml version="1.0" encoding="utf-8"?&
&!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&
&hibernate-configuration&
&session-factory name="sessionFactory"&
&!-- 指定连接数据库所用的驱动 --&
&property name="hibernate.connection.driver_class"&oracle.jdbc.driver.OracleDriver&/property&
&!-- 指定连接数据库的url,hibernate连接的数据库名 --&
&property name="hibernate.connection.url"&jdbc:oracle:thin:@localhost:1521:XE&/property&
&property name="hibernate.connection.useUnicode"&true&/property&
&property name="hibernate.connection.characterEncoding"&gbk&/property&
&!-- 指定连接数据库的用户名 --&
&property name="hibernate.connection.username"&system&/property&
&!-- 指定连接数据库的密码 --&
&property name="hibernate.connection.password"&password&/property&
&!-- 指定连接池里最大连接数 --&
&property name="hibernate.c3p0.max_size"&20&/property&
&!-- 指定连接池里最小连接数 --&
&property name="hibernate.c3p0.min_size"&5&/property&
&!-- 指定连接池里连接的超时时长,以秒为单位 --&
&property name="hibernate.c3p0.timeout"&120&/property&
&!-- 指定连接池里最大缓存多少个Statement对象 --&
&property name="hibernate.c3p0.max_statements"&100&/property&
&!-- 每隔XX秒检查连接池里的空闲连接 ,单位是秒 --&
&property name="hibernate.c3p0.idle_test_period"&120&/property&
&!-- 当连接池里面的连接用完的时候,C3P0一次获取的新的连接数 --&
&property name="hibernate.c3p0.acquire_increment"&2&/property&
&!-- 指定数据库方言 --&
&property name="dialect"&org.hibernate.dialect.OracleDialect&/property&
&!-- 显示Hibernate持久化操作所生成的SQL --&
&property name="show_sql"&true&/property&
&!-- 将SQL脚本进行格式化后再输出 --&
&property name="hibernate.format_sql"&true&/property&
&!-- 罗列所有的映射文件 --&
&mapping resource="pojo/BillingDetails.hbm.xml" /&
&mapping resource="pojo/User.hbm.xml" /&
&/session-factory&
&/hibernate-configuration&
pojo/User.java:
import java.io.S
import java.util.HashS
import java.util.S
public class User implements Serializable {
private Set&BillingDetails& billingDetailSet = new HashSet&BillingDetails&();
public String getId() {
@SuppressWarnings("unused")
private void setId(String id) {
public String getName() {
public void setName(String name) {
this.name =
public int getAge() {
public void setAge(int age) {
this.age =
public String getPassword() {
public void setPassword(String password) {
this.password =
public Set&BillingDetails& getBillingDetailSet() {
return billingDetailS
public void setBillingDetailSet(Set&BillingDetails& billingDetailSet) {
this.billingDetailSet = billingDetailS
public void addBillingDetail(BillingDetails b){
this.billingDetailSet.add(b);
b.setUser(this);
pojo/User.hbm.xml:
&?xml version="1.0" encoding="UTF-8"?&
&!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&
&hibernate-mapping package="pojo"&
&class name="User" table="A_USER" dynamic-insert="true" dynamic-update="true"&
&id name="id" column="ID" type="string"&
&generator
&property name="name" column="NAME" type="string"/&
&property name="age" column="AGE" type="integer"/&
&property name="password" column="PASSWORD" type="string"/&
&!-- 注意这里 --&
&set name="billingDetailSet" inverse="true" cascade="save-update"&
&key column="USER_ID" not-null="true"/&
&one-to-many /&
&/hibernate-mapping&
pojo/BillingDetails.java:
import java.io.S
public abstract class BillingDetails implements Serializable{
public String getId() {
@SuppressWarnings("unused")
private void setId(String id) {
public String getOwner() {
public void setOwner(String owner) {
this.owner =
public User getUser() {
public void setUser(User user) {
this.user =
pojo/BankAccount.java:
public class BankAccount extends BillingDetails {
public String getAccount() {
public void setAccount(String account) {
this.account =
public String getBankname() {
public void setBankname(String bankname) {
this.bankname =
public String getSwift() {
public void setSwift(String swift) {
this.swift =
pojo/CreditCard.java:
public class CreditCard extends BillingDetails {
private String expM
private String expY
public String getNumber() {
public void setNumber(String number) {
this.number =
public String getExpMonth() {
return expM
public void setExpMonth(String expMonth) {
this.expMonth = expM
public String getExpYear() {
return expY
public void setExpYear(String expYear) {
this.expYear = expY
pojo/BillingDetails.hbm.xml:
&?xml version="1.0" encoding="UTF-8"?&
&!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&
&hibernate-mapping package="pojo"&
&class name="BillingDetails" abstract="true" dynamic-insert="true" dynamic-update="true"&
&id name="id" column="ID" type="string"&
&generator
&property name="owner" column="OWNER" type="string"/&
&!-- 注意这里 --&
&many-to-one name="user" column="USER_ID" /&
&union-subclass name="BankAccount" table="A_BANKACCOUNT"&
&property name="account" column="ACCOUNT" type="string"/&
&property name="bankname" column="BANKNAME" type="string"/&
&property name="swift" column="SWIFT" type="string"/&
&/union-subclass&
&union-subclass name="CreditCard" table="A_CREDITCARD"&
&property name="number" column="NUM" type="string"/&
&property name="expMonth" column="EXPMONTH" type="string"/&
&property name="expYear" column="EXPYEAR" type="string"/&
&/union-subclass&
&/hibernate-mapping&
util/HibernateUtil.java:
import org.hibernate.SessionF
import org.hibernate.cfg.C
public class HibernateUtil {
private static SessionFactory sessionF
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable e) {
throw new ExceptionInInitializerError(e);
public static SessionFactory getSessionFactory(){
return sessionF
public static void shutdown(){
getSessionFactory().close();
util/Manager.java:
import org.hibernate.S
import org.hibernate.T
import pojo.BankA
import pojo.CreditC
import pojo.U
public class Manager {
public static void main(String[] args) {
BankAccount ba = new BankAccount();
ba.setAccount("ba111");
ba.setBankname("ba222");
ba.setOwner("ba333");
ba.setSwift("ba444");
CreditCard cc = new CreditCard();
cc.setExpMonth("5");
cc.setExpYear("2010");
cc.setOwner("ba555");
cc.setNumber("55");
User user = new User();
user.setAge(30);
user.setName("fhd");
user.setPassword("999999");
user.addBillingDetail(ba);
user.addBillingDetail(cc);
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = session.beginTransaction();
session.save(user);
session.close();
A_USER表:
A_BANKACCOUNT表:
A_CREDITCARD表:
> 本站内容系网友提交或本网编辑转载,其目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请及时与本网联系,我们将在第一时间删除内容!
hibernate之多态关联(多态的一对多,利用subclass)
下面以关联映射中映射的持久化类中声明了&subclass&为例:
pom.xml: &project xmlns=&http://maven.apache.org/POM/4.0.0& xmlns:xsi=&http://www ...
hibernate之多态关联(多态的一对多,利用joined-subclass) 下面以关联映射中映射的持久化类中声明了&joined-subclass&为例: pom.xml: &project xmlns=&http://maven.apache.org/POM/4.0.0& xmlns:xsi=&http ...
hibernate之多态关联(多态的多对一,利用any,使用每个具体类一张表的策略) 下面以关联映射中映射的持久化类中声明了any为例,注意,这个例子是单向导航的: pom.xml: &project xmlns=&http://maven.apache.org/POM/4.0.0& xmlns:xsi=&http://w ...
hibernate之多态关联(多态的多对一关联或者多态的一对一关联,利用joined-subclass)
不必做任何特别的事情来启用hibernate中的多态关联,在关联映射中指定任何被映射的持久化类的名称,然后,如果该类声明了任何&union-subclass&,&subclass&或者&joined-subclass&g ...
09:50:54 目标:引用多个父表
反模式:使用多用途外键.这种设计也叫做多态关联,或者杂乱关联.
多态关联和EAV有着相似的特征:元数据对象的名字是存储在字符串中的.
在多态关联中,父表的名字是存储在Issue_Type单独一列中,有时候这样的设计被称作:混合数据与原数据.
多态关联--异构对象聚合技术 将不同的类(表)聚合(关联)起来 不同的类表现不同,但是具有某些共同的特征 下面来举一个例子,对于三个不同的类, Article, Sound, Image,他们的类型不同,表现形式不同,也就是对应表的结构不同: 创建一个rails应用 rails polymorphic 新建三个模型类Article, Sound, Image ...
上次的博文Hibernate从入门到精通(八)一对多单向关联映射中,我们讲解了一下一对多单向映射的相关内容,这次我们讲解一下一对多双向映射的相关内容. 一对多双向关联映射
一对多双向关联映射,即在一的一端存在多的一端的一个集合对象,在多的一端存在一的一端的一个对象,这样就可以保证在加载一的一端或多的一端将被指向端的集合或对象加载上来 ...
刚才看了李妙妙的这篇文章 感觉说的不是很明白, 并没有从面向对象的角度说清楚,我的理解: 要理解多态关联,首先要理解什么是多态. 多态是面向对象一个特性. 同一个对象有不同的行为,叫多态. 在李举的那个例子里, article,image, sound,这三个model会有很多共同的行为, 如果没有使用多态关联,那么每个model里面,会有相同的行为,如果你 ...}

我要回帖

更多关于 电信机顶盒灯一蓝一红 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信