温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks

#

Exception in thread "main" java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

不合法的状态,beanFactory没有初始化或者关闭,在上下文中刷新。

解决方法:ApplicationContext context = new ClassPathXmlAppliction("这里的参数没有写");


posted @ 2010-10-14 13:28 雪山飞鹄 阅读(7346) | 评论 (0)编辑 收藏


实体
Husband
package com.hibernate.one2one.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name
="husband")
public class Husband {
    
    
private int id;
    
private String name;
    
private Wife wife;
    @Id
    @GeneratedValue(strategy
=GenerationType.AUTO)
    @Column(name
="id")
    
public int getId() {
        
return id;
    }
    
public void setId(int id) {
        
this.id = id;
    }
    @Column(name
="name")
    
public String getName() {
        
return name;
    }
    
public void setName(String name) {
        
this.name = name;
    }
    @OneToOne
    @PrimaryKeyJoinColumn
    
public Wife getWife() {
        
return wife;
    }
    
public void setWife(Wife wife) {
        
this.wife = wife;
    }
    
}
Wife
package com.hibernate.one2one.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name
="wife")
public class Wife {
    
    
private int id;
    
private String name;
    
private Husband husband;
    @Id
    @Column(name
="id")
    
public int getId() {
        
return id;
    }
    
public void setId(int id) {
        
this.id = id;
    }
    @Column(name
="name")
    
public String getName() {
        
return name;
    }
    
public void setName(String name) {
        
this.name = name;
    }
    @OneToOne(optional
=false)
    @PrimaryKeyJoinColumn
    
public Husband getHusband() {
        
return husband;
    }
    
public void setHusband(Husband husband) {
        
this.husband = husband;
    }
    
}
温馨提示:注意wife.java里面的@OneToOne(optional=false)   optional=false  属性会在wife这端添加一个外键约束
添加上上述属性使用hbm2ddl导出表,打印出的sql语句
alter table wife 
        
drop 
        
foreign key FK37AF11D67CB035

    
drop table if exists husband

    
drop table if exists wife

    
create table husband (
        id 
integer not null auto_increment,
        name 
varchar(255),
        
primary key (id)
    )

    
create table wife (
        id 
integer not null,
        name 
varchar(255),
        
primary key (id)
    )

    
alter table wife 
        
add index FK37AF11D67CB035 (id), 
        
add constraint FK37AF11D67CB035 
        
foreign key (id) 
        
references husband (id)

@Test
    
public void insert(){
        Session session
=HibernateSessionFactory.getSession();
        Transaction transaction
=session.beginTransaction();
        
try {
            transaction.begin();
            Husband husband
=new Husband();
            husband.setName(
"小明");
            session.save(husband);
            Wife wife
=new Wife();
            wife.setName(
"如花");
            wife.setHusband(husband);
            wife.setId(husband.getId());
            session.save(wife);
            transaction.commit();
        } 
catch (HibernateException e) {
            e.printStackTrace();
            transaction.rollback();
        }
    }
@Test
    
public void insert(){
        Session session
=HibernateSessionFactory.getSession();
        Transaction transaction
=session.beginTransaction();
        
try {
            transaction.begin();
            Husband husband
=new Husband();
            husband.setName(
"小明");
            session.save(husband);
            Wife wife
=new Wife();
            wife.setName(
"如花");
            wife.setHusband(husband);
            wife.setId(husband.getId());
            session.save(wife);
            transaction.commit();
        } 
catch (HibernateException e) {
            e.printStackTrace();
            transaction.rollback();
        }
    }
温馨提醒:此处必须同时设置
wife.setHusband(husband);
wife.setId(husband.getId());
否则报org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

示例程序
posted @ 2010-10-14 10:28 雪山飞鹄 阅读(2225) | 评论 (1)编辑 收藏


alter table wife
        drop
        foreign key FK37AF11D67CB035

    drop table if exists husband

    drop table if exists wife

    create table husband (
        id integer not null auto_increment,
        name varchar(255),
        primary key (id)
    )

    create table wife (
        id integer not null,
        name varchar(255),
        primary key (id)
    )

    alter table wife
        add index FK37AF11D67CB035 (id),
        add constraint FK37AF11D67CB035
        foreign key (id)
        references husband (id)

实体
Husband

private int id;
 private String name;
 private Wife wife;

Wife
private int id;
 private String name;
 private Husband husband;

Husband.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
    
<hibernate-mapping package="com.hibernate.one2one.bean">
        
<class name="Husband" table="husband">
            
<id name="id" column="id">
                
<generator class="native"></generator>
            
</id>
            
<property name="name"></property>
            
<one-to-one name="wife" cascade="all" class="Wife"></one-to-one>
        
</class>
    
</hibernate-mapping>

Wife.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
    
<hibernate-mapping package="com.hibernate.one2one.bean">
        
<class name="Wife" table="wife">
            
<id name="id" column="id">
                
<generator class="foreign">
                    
<param name="property">husband</param>
                
</generator>
            
</id>
            
<property name="name"></property>
            
<one-to-one name="husband" constrained="true"></one-to-one>
        
</class>
    
</hibernate-mapping>
@Test
    
public void insert(){
        Session session
=HibernateSessionFactory.getSession();
        Transaction transaction
=session.beginTransaction();
        
try {
            transaction.begin();
            Husband husband
=new Husband();
            husband.setName(
"小明");
            session.save(husband);
            Wife wife
=new Wife();
            wife.setName(
"如花");
            wife.setHusband(husband);
            session.save(wife);
            transaction.commit();
        } 
catch (HibernateException e) {
            e.printStackTrace();
            transaction.rollback();
        }
    }


示例程序
posted @ 2010-10-14 10:01 雪山飞鹄 阅读(669) | 评论 (0)编辑 收藏


create table Husband
(
   id                   
int not null auto_increment,
   name                 
varchar(200),
   
primary key (id)
);
create table Wife
(
   id                   
int not null,
   name                 
varchar(20),
   
primary key (id)
);
alter table Wife add constraint FK_Reference_1 foreign key (id)
      
references Husband (id) on delete restrict on update restrict;
Wife.java
package com.jpa.one2one.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@SuppressWarnings(
"serial")
@Entity
@Table
public class Wife implements Serializable{
    
private int id;
    
private String name;
    
private Husband husband;
    @Id
    @Column
    
public int getId() {
        
return id;
    }
    
public void setId(int id) {
        
this.id = id;
    }
    @Column(name
="name")
    
public String getName() {
        
return name;
    }
    
public void setName(String name) {
        
this.name = name;
    }
    @OneToOne
    @PrimaryKeyJoinColumn
    
public Husband getHusband() {
        
return husband;
    }
    
public void setHusband(Husband husband) {
        
this.husband = husband;
    }
    
}
Husband.java
package com.jpa.one2one.bean;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table
public class Husband {
    
    
private int id;
    
private String name;
    
private Wife wife;
    @Id
    @Column
    @GeneratedValue(strategy
=GenerationType.AUTO)
    
public int getId() {
        
return id;
    }
    
public void setId(int id) {
        
this.id = id;
    }
    @Column(name
="name")
    
public String getName() {
        
return name;
    }
    
public void setName(String name) {
        
this.name = name;
    }
    @OneToOne(cascade
=CascadeType.ALL)
    @PrimaryKeyJoinColumn
    
public Wife getWife() {
        
return wife;
    }
    
public void setWife(Wife wife) {
        
this.wife = wife;
    }
    
}
HusbandDAO
package com.jpa.one2one.dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.junit.Test;

import com.jpa.one2one.bean.Husband;
import com.jpa.one2one.bean.Wife;
import com.jpa.one2one.util.JPAUtil;

public class HusbandDAO {
    
    @Test
    
public void insert(){
        EntityManager entityManager
=JPAUtil.getInstance();
        EntityTransaction transaction
=entityManager.getTransaction();
        
try {
            transaction.begin();
            Husband husband
=new Husband();
            husband.setName(
"张三");
            entityManager.persist(husband);
            Wife wife
=new Wife();
            
//wife.setHusband(husband);
            wife.setName("如花");
            wife.setId(husband.getId());
            entityManager.persist(wife);
            transaction.commit();
        } 
catch (Exception e) {
            e.printStackTrace();
            transaction.rollback();
        }
    }
}
JPAUtil
package com.jpa.one2one.util;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class JPAUtil {
    
    
private static EntityManager entityManager;
    
public static EntityManager getInstance(){
        
if(entityManager!=null){
            
return entityManager;
        }
else{
            
return makeInstance();
        }
    }
    
private static synchronized EntityManager makeInstance() {
        
if(entityManager==null){
            EntityManagerFactory entityManagerFactory
=Persistence.createEntityManagerFactory("JPAPU");
            
return entityManagerFactory.createEntityManager();
        }
        
return null;
    }
}
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
 version="1.0">
    
    
<persistence-unit name="JPAPU" transaction-type="RESOURCE_LOCAL">
        
<provider>org.hibernate.ejb.HibernatePersistence</provider>
        
<class>com.jpa.one2one.bean.Wife</class>
        
<class>com.jpa.one2one.bean.Husband</class>
          
<properties>
            
<property name = "hibernate.connection.driver_class" value = "com.mysql.jdbc.Driver"/>
            
<property name = "hibernate.connection.url" value = "jdbc:mysql://localhost:3306/JPA"/>
            
<property name = "hibernate.connection.username" value = "root"/>
            
<property name = "hibernate.connection.password" value = "root"/>
            
<property name="hibernate.show_sql" value="true"/>
            
<property name="hibernate.format_sql" value="true"/>
          
</properties>
    
</persistence-unit>
  
</persistence>

示例程序
posted @ 2010-10-14 09:23 雪山飞鹄 阅读(3673) | 评论 (0)编辑 收藏

     摘要: 环境:         ibatis-2.3.4.726         使用ibatis2最小jar包配置         commons-collec...  阅读全文
posted @ 2010-10-13 12:06 雪山飞鹄 阅读(2747) | 评论 (1)编辑 收藏

Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource、@PostConstruct以及@PreDestroy。
  @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
  @Resource装配顺序
  1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常
  2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常
  3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常
  4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;
posted @ 2010-10-11 16:52 雪山飞鹄 阅读(45098) | 评论 (6)编辑 收藏

According to TLD or attribute directive in tag file, attribute test does not accept any expressions

2.4及以后写成(JSTL1.1) 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

2.3及以前(JSTL1.0)
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core" %> 
posted @ 2010-10-10 15:07 雪山飞鹄 阅读(229) | 评论 (0)编辑 收藏

java.lang.NoSuchMethodError: javax.persistence.OneToOne.orphanRemoval()Z
ejb3的jar包与JPA包冲突 ejb3的jar包删除就OK了

java.lang.NoClassDefFoundError: javax/persistence/Cacheable
错误原因,javax.persistence.Cacheable 是 JPA 2.0 规范中的东西!
支持 JPA 2 的 Hibernate 实现 3.5 版目前还处于CR2 版本之中,还没有正式发布。
需要在加入hibernate-distribution-3.5.0-Final\lib\jpa目录下的hibernate-jpa-2.0-api-1.0.0.Final.jar

java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
缺少slf4j-nop-1.5.8.jar
posted @ 2010-10-09 10:42 雪山飞鹄 阅读(1636) | 评论 (1)编辑 收藏

     摘要:   使用Xdoclet和Ant构建Hibernate映射和配置文件 温馨提示:由于文档中含大量图片,这里不方便一一上传建议下载本文电子版文档阅读 Xdoclet.pdf 本文工程下载 svn地址:http://xdocletdemo.googlecode.com/svn/trunk/ 功能描述:       &...  阅读全文
posted @ 2010-09-30 16:03 雪山飞鹄 阅读(2970) | 评论 (3)编辑 收藏

1. PHP

PHP Cheat Sheet

http://www.addedbytes.com/cheat-sheets/php-cheat-sheet/

2. MySQL

MySQL Cheat Sheet

http://www.addedbytes.com/cheat-sheets/mysql-cheat-sheet/

3. JavaScript

JavaScript Cheat Sheet

http://www.addedbytes.com/cheat-sheets/javascript-cheat-sheet/

4. CSS

CSS Cheat Sheet

http://www.addedbytes.com/cheat-sheets/css-cheat-sheet/

5. Regular Expressions

 

Regular Expressions Cheat Sheet

http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/

6. Apache’s mod_rewrite

mod_rewrite Cheat Sheet

http://www.addedbytes.com/apache/mod_rewrite-cheat-sheet/

7. jQuery

jQuery Cheat Sheet

http://acodingfool.typepad.com/blog/pdf/jquery_1.3_cheatsheet_v1.pdf

8. HTML

HTML Cheat Sheet

http://www.addedbytes.com/cheat-sheets/html-cheat-sheet/

9. HTML Character Entities

HTML Entities Cheat Sheet

http://www.addedbytes.com/cheat-sheets/html-character-entities-cheat-sheet/

10. RGB Hex Color Codes

RGB Color Codes Cheat Sheet

http://www.addedbytes.com/cheat-sheets/colour-chart/

11. .htaccess

.htaccess Cheat Sheet

http://www.thejackol.com/htaccess-cheatsheet/

12. Apache

Apache Cheat Sheet

http://www.petefreitag.com/cheatsheets/apache/

13. SEO

SEO Cheat Sheet

http://www.seomoz.org/blog/the-web-developers-seo-cheat-sheet

14. Gmail

Gmail Cheat Sheet

http://www.marcofolio.net/cheat_sheets/gmail_keyboard_shortcuts_cheat_sheet.html

15. HTML5

HTML5 Cheat Sheet

http://www.smashingmagazine.com/2009/07/06/html-5-cheat-sheet-pdf/

16. Google Analytics

Google Analytics Cheat Sheet

http://www.searchenginejournal.com/the-huge-collection-of-google-analytics-tips/7426/

17. WordPress Cheat Sheets

WordPress Cheat Sheets

http://speckyboy.com/2009/06/17/14-essential-wordpress-development-and-design-cheat-sheets/

18. Subversion

Subversion Cheat Sheet

http://www.addedbytes.com/cheat-sheets/subversion-cheat-sheet/

19. Eclipse

Eclipse Cheat Sheet

http://refcardz.dzone.com/refcardz/getting-started-eclipse?oid=hom7187

20. Google Search Engine

Google Cheat Sheet

http://www.google.com/help/cheatsheet.html

21. Twitter

Twitter Cheat Sheet

http://jasontheodor.com/2009/04/28/twitter-tweet-sheet-2/

22. CakePHP

CakePHP Cheat Sheet

http://cakephp.org/files/Resources/CakePHP-1.2-Cheatsheet.pdf

23. Joomla

Joomla 1.5 Cheat Sheet

http://www.younic.de/joomla-basic-template-cheatsheet

24. CodeIgniter

CodeIgniter Cheat Sheet

http://designfellow.com/blog/freebies/codeigniter-quick-reference-cheat-sheet-version-2-0-released/

25. Drupal

Drupal Cheat Sheet

http://www.inmensia.com/files/pictures/internal/CheatSheetDrupal4.7.png

26. Firebug

Firebug Cheat Sheet

http://duvet-dayz.com/firebug-cheatsheet/

posted @ 2010-09-29 17:18 雪山飞鹄 阅读(1101) | 评论 (1)编辑 收藏

仅列出标题
共22页: First 上一页 8 9 10 11 12 13 14 15 16 下一页 Last