To address the issues of data annotation scarcity under small-sample conditions and domain shift in cross-domain applications in fault diagnosis, a domain adaptation strategy integrating contrastive learning was proposed. First, a hierarchical feature alignment mechanism was adopted to jointly optimize domain-level adversarial adaptation and class-level contrastive learning. This mechanism balanced the transfer of domain-invariant features and the preservation of class discriminability, thereby improving the fault classification accuracy in the target domain. Second, a one-dimensional domain-shared convolutional neural network(CNN) was constructed to extract the common features of data from the source domain and target domain. By combining the unsupervised domain discriminative loss with a gradient reversal layer to optimize domain-level adaptation, the transferability of domain-invariant characteristics was enhanced. Finally, an unsupervised contrastive loss function was designed to optimize class-specific information. This function constrains the intra-class compactness and inter-class separability in the feature space, enabling class-level alignment under limited samples and effectively suppressing misidentification of class boundaries. The cross-condition unsupervised experimental results on both the constructed planetary gearbox test rig dataset and the public bearing dataset show that the diagnostic accuracy generally exceeds 95% in multiple cross-condition transfer tasks. Compared with several unsupervised domain adaptation methods, the proposed method achieves a significant performance improvement, validating its effectiveness and superiority in small-sample unsupervised cross-condition fault diagnosis.