package com.brandmaker.mms.service.portal.test;
import static org.junit.Assert.assertNotEquals;
import java.util.Iterator;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.ForeignKey;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* Created by alexandr on 02.02.15.
*/
public class HibernateForeignKeyTest extends BaseCoreFunctionalTestCase
{
private static final String USER_ID = "USER_ID";
private static final String PERSONAL_DATA_ID = "PERSONAL_DATA_ID";
private static final String USER_SETTINGS_TABLE_NAME = "user_settings";
private static final String USERS_TABLE_NAME = "users";
private static final String PERSONALDATA_TABLE_NAME = "personal_data";
private static final String EXPLICIT_USER_SETTING_FK_NAME = "FK_USER_SETTING_TO_USER";
private static final String EXPLICIT_USERS_FK_NAME = "FK_USER_SETTING_TO_USER_BACK";
private static final String EXPLICIT_PD_FK_NAME = "FK_PERSONAL_DATA_TO_USER";
@Entity
@Table( name = PERSONALDATA_TABLE_NAME )
public static class PersonalData
{
@Column(name = PERSONAL_DATA_ID, nullable = false)
private Integer fakeId;
@Id
@Column(name = USER_ID, nullable = false, insertable = false, updatable = false)
private Integer userId;
@OneToOne(optional = false, fetch = FetchType.LAZY, targetEntity = User.class)
@JoinColumn(name = USER_ID, nullable = false, updatable = false)
@ForeignKey(name = EXPLICIT_PD_FK_NAME)
private User user;
}
@Entity
@Table( name = USER_SETTINGS_TABLE_NAME )
public static class UserSetting {
@Id
@GeneratedValue
@Column(name = USER_ID, nullable = false)
public long id;
@OneToOne(optional = false, fetch = FetchType.LAZY, targetEntity = User.class)
@JoinColumn(name = USER_ID, nullable = false, updatable = false)
@ForeignKey(name = EXPLICIT_USER_SETTING_FK_NAME)
private User user;
}
@Entity
@Table( name = USERS_TABLE_NAME )
public static class User {
@Id
@GeneratedValue
@Column(name = USER_ID)
public long id;
@OneToOne(fetch = FetchType.LAZY, optional = false, targetEntity = PersonalData.class)
@JoinColumn(name = USER_ID)
public PersonalData personalData;
@OneToOne(fetch = FetchType.LAZY, optional = false, targetEntity = UserSetting.class)
@JoinColumn(name = USER_ID)
@ForeignKey(name = EXPLICIT_USERS_FK_NAME)
public UserSetting userSetting;
}
@Test
public void testForeignKeyUsers(){
Iterator<org.hibernate.mapping.Table> tableItr = configuration().getTableMappings();
while (tableItr.hasNext())
{
org.hibernate.mapping.Table table = tableItr.next();
if (USERS_TABLE_NAME.equals(table.getName())){
Iterator fkItr = table.getForeignKeyIterator();
String foreinKeyNameForUserSetting = null;
String foreinKeyNameForPersonalData = null;
while (fkItr.hasNext())
{
org.hibernate.mapping.ForeignKey fk = (org.hibernate.mapping.ForeignKey) fkItr.next();
if ("com.brandmaker.mms.service.portal.test.HibernateForeignKeyTest$UserSetting".equals(fk.getReferencedEntityName()))
{
foreinKeyNameForUserSetting = fk.getName();
}
if ("com.brandmaker.mms.service.portal.test.HibernateForeignKeyTest$PersonalData".equals(fk.getReferencedEntityName()))
{
foreinKeyNameForPersonalData = fk.getName();
}
}
assertNotEquals(foreinKeyNameForPersonalData, foreinKeyNameForUserSetting);
}
}
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
User.class, UserSetting.class, PersonalData.class
};
}
}