You can do like this , with the assumption your class A and B look like this :
Class A{
String userId;
String a;
String b;
}
Class B{
User user;
String a;
String b;
}
Class User{
String userId;
}
You have add below mapping in your mapping ".xml" file.
<mapping>
<class-a>com.techidiocy.example.A</class-a>
<class-b>com.techidiocy.example.B</class-b>
<field>
<a>userId</a>
<b>user.userId</b>
</field>
</mapping>