比如有student类和card(学生证)类,两者都定义持有对方的一个引用。class Student{
private int id;
private int age;
private String name;private Card card;
...
}
class Card{
private int cardid;
private String name;
private String stu_id;
private Student stu;
...
}
假定有get和set方法了。那么这种情况,实际的对象里面不是一个有另一个,而另一个有指向它,如此不是循环没完了吗?晕了,想想都头疼,请问这里面是怎么构造的,实际的对象里面又有什么?它们的垃圾回收又怎么回事? 这种情况有的,比如Hibernate 里面的一对一双向关联映射。
private int id;
private int age;
private String name;private Card card;
...
}
class Card{
private int cardid;
private String name;
private String stu_id;
private Student stu;
...
}
假定有get和set方法了。那么这种情况,实际的对象里面不是一个有另一个,而另一个有指向它,如此不是循环没完了吗?晕了,想想都头疼,请问这里面是怎么构造的,实际的对象里面又有什么?它们的垃圾回收又怎么回事? 这种情况有的,比如Hibernate 里面的一对一双向关联映射。
不懂! 我被互相持有弄晕了,一个student对象里有card,而card对象里有student,......反反复复,到底这对象里面有什么啊,或者谁帮我画个内存状态图理解一下?高手别吝赐教啊,谢谢了。
不用管这么多,其实就是hibernate中的一对一双向外键关联.你完全可以去掉一个外键用@OneToOne(mappedBy="")注解
还不懂我可以看看我博客http://hi.baidu.com/martian_ma/blog/item/17fd552ba1d3b5335243c12e.html
我也刚学, 一对一双向外键关联和生成的表无关,只是从表到对象是双向的而已.
public setCard(Card card){
this.card=card;
}
private Card card;
...
}
class Card{
public setStu(Student stu){
this.stu=stu;
}
private Student stu;
...
}
main:
{
Card c= new Card();
Student s=new Student();
c.setStu(s);
s.setCard(c);
}
http://www.cnblogs.com/healerkx/category/171828.htmlIE6和IE7,当有循环引用的时候,js就可能泄露,而firefox就不会不过真实的Java,C#的gc的泄露,不是机制问题,是逻辑问题。
Going from strongest to weakest, the different levels of reachability reflect the life cycle of an object. They are operationally defined as follows:
An object is strongly reachable if it can be reached by some thread without traversing any reference objects. A newly-created object is strongly reachable by the thread that created it.
An object is softly reachable if it is not strongly reachable but can be reached by traversing a soft reference.
An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing a weak reference. When the weak references to a weakly-reachable object are cleared, the object becomes eligible for finalization.
An object is phantom reachable if it is neither strongly, softly, nor weakly reachable, it has been finalized, and some phantom reference refers to it.
Finally, an object is unreachable, and therefore eligible for reclamation, when it is not reachable in any of the above ways.来自http://java.sun.com/javase/6/docs/api/java/lang/ref/package-summary.html#reachability