HEXH's Blog

面朝大海,春暖花开


  • 首页

  • 分类

  • 标签

  • 归档

  • 公益404

Unsafe

发表于 2013-10-17   |   分类于 java   |  

### 如何使用Unsafe类呢?

  • 方式一:通过Unsafe提供的工厂方法。
    Unsafe unsafe = Unsafe.getUnsafe(); 通过这样的方式获得Unsafe的实力会抛出异常信息,因为在unsafe的源码中会有对安全性的检查
  public static Unsafe getUnsafe() {
Class cc = sun.reflect.Reflection.getCallerClass(2);
if (cc.getClassLoader() != null)
throw new SecurityException("Unsafe");
return theUnsafe;
}

Exception in thread "main" java.lang.SecurityException: Unsafe
at sun.misc.Unsafe.getUnsafe(Unsafe.java:68)
at org.wk.core.concurrent.InitUnsafe.main(InitUnsafe.java:12)
  • 方式二:通过反射的方式。
    因为在开源版本的Unsafe.java中声明了一个实例域,所以我们可以通过反射的方式来获得这个域。
  private static final Unsafe theUnsafe = new Unsafe();

//使用方法
private static Unsafe getUnsafeInstance() throws SecurityException,
NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {

Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafeInstance.setAccessible(true);
return (Unsafe) theUnsafeInstance.get(Unsafe.class);
}
  • 事例
private static Unsafe unsafe = null;
static {
try {
Class<?> clazz = Unsafe.class;
Field f;
f = clazz.getDeclaredField("theUnsafe");
f.setAccessible(true);
unsafe = (Unsafe) f.get(clazz);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
  • 结束
阅读全文 »

hibernate spring

发表于 2013-10-10   |   分类于 hibernate   |  

### When using spring and spring managed transactions never mess around with the 'hibernate.current_session_context_class' UNLESS you are using JTA.

Spring will by default set its own CurrentSessionContext implementation (the SpringSessionContext), however if you set it yourself this will not be the case. Basically breaking proper transaction integration.

The only reason for chancing this setting is if you want to use JTA managed transactions, then you have to setup this to properly integrate with JTA.

阅读全文 »

spring bootstrap

发表于 2013-10-10   |   分类于 spring   |  
  • Bootstrap a Web Application with Spring 3
阅读全文 »

hibernate-id-accessType

发表于 2013-08-06   |   分类于 hibernate   |  

### @Id

一般使用GenerationType.AUTO即可。
GenerationType.AUTO - identity, sequence, or hilo
GenerationType.IDENTITY - MS SQL Server, MySQL
GenerationType.SEQUENCE - Oracle

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "...")
private Long getId() {
return id;
}

### @AccessType
Hibernate存取Domain object的数据有两种方式,一是透过getter与setter method(access by property),二是直接存取field(access by field),通常是建议使用前者,有封装的作用。

在Hibernate Annotation中决定存取方式的规则如下:

  • 若设有@AccessType,则采用@AccessType之配置。
  • 不然则以@Id所在的位置决定,若@Id在property上,则采用access by property,若@Id在field上,则采用access by field。
  • @AccessType可放在class、field或property上,在class上表示配置为class level,整个class套用,在field或property上则为attribute level,仅该attribute(property或field)适用。

不管是透过@AccessType配置或@Id所在位置决定,若class配置为access by property,要变更特定的attribute为access by field时,必须将@AccessType(“field”)放在getter method上。

不管是透过@AccessType配置或@Id所在位置决定,若class配置为access by field,要变更特定的attribute为access by property时,必须将@AccessType(“property”)放在field上。

最后,@Embedded与@MappedSuperclass的存取规则均同于owning/inherited entity。

阅读全文 »

hibernate

发表于 2013-08-05   |   分类于 hibernate   |  

### 关联

@OneToMany mappedBy
@ManyToOne 默认fetch:EAGER @JoinColumn
hibernate 自动建表的关联一般是外键,但实际表结构不一定是外键
### 删除
建议先load后删除,方便使用CascadeType.REMOVE
### load get
load方式检索不到的话会抛出org.hibernate.ObjectNotFoundException异常
get方法检索不到的话会返回null
### a different object with the same identifier value was already associated with the session
session.clean()
session.refresh(object)
session.merge(object)
### megre
megre 可能也更新关联的对象
### Arraytest

  • 当关联的不是entity对象时可以用@ElementCollection
  • @ElementCollection关联的是enmu时,默认是tinyint,可以使用@Enumerated(EnumType.STRING)在表中用字符
  • 当你的Enum中有自定义字段,并且你希望用该字段作为hibernate持久化的值的时候,就需要用到hibernate的自定义映射类型UserType
  • 有序的对象可以使用@OrderColumn,@IndexColumn
  • @CollectionId可以给*@ElementCollection*添加ID
    ### @IdClass
  • pk可以是关联对象的字段,可应通过@IdClass引用
  • @ID 可以加在@ManyToOne等上面
  • @MapsId 在@EmbeddedId中启到相同的作用 也可以使用@JoinColumns
  • SimpleParentEmbedded可以不用@IdClass
    ### CascadeTest
  • DETACH对应缓存的数据的删除等。
    ### 时间
  • @Temporal指定是Date,Time,Timestamp
    ### @Basic
  • 默认的为@Basic
    ### @Parent
  • IdClass可以用@Parent引用owner
    ### @XXXToOne
  • 默认@JoinColumn id attribute mapped by join column default
    ### 继承
  • 查询@org.hibernate.annotations.Entity(polymorphisms = PolymorphismType.EXPLICIT)
  • 或者Restrictions.eq("class", A.class)
    ### left join
  • createAlias("menus", "menus", Criteria.LEFT_JOIN)
    ### 其他
  • man
  • JPA注解
阅读全文 »

memo

发表于 2013-08-04   |   分类于 trivial   |  

# google dns
8.8.8.8
8.8.4.4
# appspot
74.125.128.141
# hexh2003-hrd.appspot.com
# google cdn
203.208.46.146 dl.google.com
203.208.46.146 dl-ssl.google.com

阅读全文 »

spring aspect

发表于 2013-08-02   |   分类于 spring   |  

当雷同连接点上应用了多个aspect时,aspect的优先级是不明确的,除非显式地指定它们的优先级。
aspect的优先级可以通过现实Ordered口接或者用使@Order注解现实。
spring xml 配置中可以设置order属性

阅读全文 »

gwt startup

发表于 2013-07-30   |   分类于 java   |  

安装google gwt plugin

update site http://dl.google.com/eclipse/plugin/${eclipse.version}

建立maven gwt project

mvn archetype:generate  \
-DarchetypeGroupId=org.codehaus.mojo \
-DarchetypeArtifactId=gwt-maven-plugin \
-DarchetypeVersion=2.5.1

修改project的pom.xml

阅读全文 »

getResource

发表于 2013-07-23   |   分类于 java   |  

Class.getResource 起始位置为.class文件所在的文件夹。 以/开头的路径起始位置切换到工作目录下。

ClassLoader.getResource的起始位置为工作目录。路径不能以/开头。

spring 的Resource路径,表示绝对路径时用两个//开头。

阅读全文 »

ubuntu apt-get

发表于 2013-06-03   |   分类于 ubuntu   |  

### apt-get

  • update
    在修改/etc/apt/sources.list或/etc/apt/preferences之後运行该命令。此外您需要定期运行这一命令以确保您的软件包列表是最新的。
  • upgrade
    apt-get upgrade
    可以使用这条命令更新软件包,apt-get upgrade不仅可以从相同版本号的发布版中更新软件包,也可以从新版本号的发布版中更新软件包,尽管实现后一种更新的推荐命令为apt-get dist-upgrade;
    在运行apt-get upgrade命令时加上-u选项很有用(即:apt-get -u upgrade)。这个选项让APT显示完整的可更新软件包列表。不加这个选项,你就只能盲目地更新。APT会下载每个软件包的最新更新版本,然后以合理的次序安装它们。注意在运行该命令前应先运行 apt-get update更新数据库。更新任何已安装的软件包。[1]
  • dist-upgrade
    将系统升级到新版本
  • install
    apt-get install packagename
    安装一个新软件包
  • remove
    apt-get remove packagename
    卸载一个已安装的软件包(保留配置文档)
    apt-get --purge remove packagename
    卸载一个已安装的软件包(删除配置文档)
  • autoremove
    apt-get autoremove packagename
    同上,比上面的要删除的彻底一点
  • autoclean
    apt-get autoclean
    apt会把已装或已卸的软件都备份在硬盘上,所以假如需要空间的话,能够让这个命令来删除您已删掉的软件
  • clean
    apt-get clean
    这个命令会把安装的软件的备份也删除,但是这样不会影响软件的使用。
阅读全文 »
1…789
Xuehui He

Xuehui He

面朝大海,春暖花开

83 日志
23 分类
13 标签
RSS
github
© 2013 - 2016 Xuehui He
由 Hexo 强力驱动
主题 - NexT.Mist