Baur&King在他们的书中说:
隐式连接始终沿着多对一或一对一关联,而不是通过集合值关联.
[P 646,Ch 14]
但是当我在代码中执行此操作时,它会生成CROSS JOIN而不是INNER JOIN.
映射来自Member2(多对一) - > CLub.
但Club2没有关于会员的信息,并且Member2拥有Club2的外键.
我的疑问是
// Implicit: Find all UK club member who is female
Transaction t1 = HibernateUtil.begin();
Query query =
HibernateUtil.getSession().createQuery("From Member2 m where m.club2.country = 'UK' ");
List<Member2> memList = query.list();
for (Member2 m : memList)
System.out.println(m);
HibernateUtil.end(t1);
Run Code Online (Sandbox Code Playgroud)
并且,Hibernate生成以下SQL查询:
Hibernate:
select
member2x0_.member_id as member_i1_1_,
member2x0_.club_id as club_id5_1_,
member2x0_.member_age as member_a2_1_,
member2x0_.member_name as member_n3_1_,
member2x0_.member_sex as member_s4_1_
from
TBL_MEMBER2 member2x0_ cross
join …Run Code Online (Sandbox Code Playgroud) 我试图用反射打破BillPaugh Singleton解决方案,我能够做到但我可以在访问BillPaughSingleTon解决方案时看到两个构造函数.为什么这样 ?另外通过反复试验发现HelperClass中的行是造成这种情况的.为什么呢?
BillPaughClass
package creational.BillPaugh;
public class SingleTonBillPaugh
{
private SingleTonBillPaugh instance;
public static SingleTonBillPaugh getInstance()
{
return SingleTonHelper.instance;
}
private SingleTonBillPaugh()
{
System.out.println(Thread.currentThread().getName() + " instance is going to be created");
}
static class SingleTonHelper
{
private static final SingleTonBillPaugh instance = new SingleTonBillPaugh(); //if we remove this line, multiple constructor will not be there. But this line is needed for singleton.
}
}
Run Code Online (Sandbox Code Playgroud)
使用Reflection破坏SingleTon.
package creational.BillPaugh;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class BreakBillPaughUsingReflection
{
public static void …Run Code Online (Sandbox Code Playgroud) 我试图找出(出于好奇)哪个线程实际上使单个构造函数被调用.好吧,我完全明白,每次执行Test程序时线程都会有所不同,只是想务实地检查一下.我试过了
dumpStack()
printStackTrace()
getStackTrace()
but the does not give any hint of other class (!phew)
Run Code Online (Sandbox Code Playgroud)
然后尝试通过放置wait()来停止最后一行的线程,以便使用jvisualvm检查线程转储,令人惊讶的是,我想要的信息也在那里丢失.
谁能告诉我如何为此实现解决方案?
Singleton Class,其getInstance()方法是从每个线程(10)的run()方法调用的.但是对于只有一个Thread调用,实际的构造函数将运行.
package creational;
public class SingleTonBillPaugh
{
private SingleTonBillPaugh instance;
public static SingleTonBillPaugh getInstance()
{
return SingleTonHelper.instance;
}
private SingleTonBillPaugh()
{
System.out.println("instance is going to be created");
}
private static class SingleTonHelper
{
private static final SingleTonBillPaugh instance = new SingleTonBillPaugh();
}
}
Run Code Online (Sandbox Code Playgroud)
单例和线程创建的测试类.注意:故意添加obj.wait()以使线程处于暂停模式,从而使它们能够在jvisualvm中进行查看.
package creational;
import java.util.concurrent.TimeUnit;
public class SingleTonBillPaughTest
{
public static void main(String[] args)
{
Thread[] t = new …Run Code Online (Sandbox Code Playgroud) 我正在做我的开发工作,并想知道在代码实践,内存和时间效率方面哪一个可能是更好的选择?
public static void printtHashmap(Map<?,?> hm) {
for(Object key : hm.keySet())
{
System.out.println(key+" "+hm.get(key));
}
}
public static void printHashMapByEntry(Map<?,?> hm) {
for(Map.Entry<?,?> entry: hm.entrySet())
{
System.out.println(entry.getKey()+" "+entry.getValue());
}
Run Code Online (Sandbox Code Playgroud) java ×4
singleton ×2
collections ×1
cross-join ×1
database ×1
hashmap ×1
hibernate ×1
map ×1
sql ×1