我在这里看到了几个linke和doc,仍然没有弄清楚问题 . 我正在迁移Spring 4.2和Hibernate 4.3.x.我坚持使用Object时面临一个基本的问题 .

@Entity
@Table(name = "CUSTOMER_TYPE")
public class CustomerType implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "CID")
@GeneratedValue(strategy = GenerationType.AUTO)
private int custTypeId;

@Column(name = "C_TYPE_NAME", nullable = false)
private String custTypeName;

@Column(name = "C_TYPE_CODE", nullable = false)
private String custTypeCode;

// setters and gettes

}

服务类:

@Service("customerTypeService")
@Transactional
public class CustomerTypeServiceImpl implements CustomerTypeService {

@Autowired
private CustomerTypeDao customerTypeDao;
  // business methods and dao access point
}

我的抽象Dao是:

public abstract class AbstractDao<PK extends Serializable, T> {
   public void persist(T entity) {
 // getSession().persist(entity.getClass().getName(), entity);
    getSession().persist(entity);
//  getSession().flush();
}

}

我通过测试数据的测试类:

AnnotationConfigApplicationContext ctx = new    AnnotationConfigApplicationContext();
    ctx.register(AppConfig.class);
    ctx.refresh();
    ctx.start();
    CustomerTypeService cust = ctx.getBean(CustomerTypeService.class);

    CustomerType customerType = new CustomerType();
    customerType.setCustTypeCode("D");
    //customerType.setCustTypeId(4);
    customerType.setCustTypeName("DOCTOR");

    cust.saveCustomerType(customerType);

Exception is: Exception in thread "main" org.hibernate.PersistentObjectException: detached entity passed to persist: com.myproject.springmvc.model.CustomerType.

EDIT Config类中的Tx Manager:

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
   HibernateTransactionManager txManager = new HibernateTransactionManager();
   txManager.setSessionFactory(s);
   return txManager;
}