Hi, learning hibernate but still experiencing some errors and exceptions ...

My User.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
 
<hibernate-mapping>
 
	<class name="com.genuitec.hibernate.User" table="SNX_USERS" >
    	<id name="userId" type="java.lang.Long" column="user_id" >
        	<generator class="increment" />
        </id>
        <property name="firstName" type="java.lang.String" column="first_name" length="20" />
        <property name="lastName" type="java.lang.String" column="last_name" length="20" />
        <property name="age" type="java.lang.Integer" column="age" length="-1" />
        <property name="email" type="java.lang.String" column="email" length="40" />

     
    	<set name = "phoneNumbers" cascade = "all">
    		<key>
     			<column name = "USER_ID" />
     			<column name = "PHONE_TYPE" />
     		</key>
     		<one-to-many class = "com.genuitec.hibernate.PhoneNumber" />
 		</set>
 	
 	</class>

</hibernate-mapping>

My PhoneNumber.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
      "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
	
<hibernate-mapping>
	
	<class name="com.genuitec.hibernate.PhoneNumber" table ="SNX_PHONE_NUMBERS" >
	
		<composite-id>
			<key-property column="USER_ID" name="userId" type="java.lang.Long" />
			<key-property column="NUMBER_TYPE" name="numberType" type="java.lang.String" />
		</composite-id>			
		
		<property name="phone" type="java.lang.Long"> 
	  		<column name="PHONE" precision="22" scale="0"/> 
		</property>	
		
	</class>
	
</hibernate-mapping>

Exception

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.MappingException: Foreign key (FKE0AAC4D7AF0E5654:SNX_PHONE_NUMBERS [USER_ID,PHONE_TYPE])) must have same number of columns as the referenced primary key (SNX_USERS [user_id])
	at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:113)
	at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:96)
	at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1296)
	at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1203)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1319)
	at com.genuitec.hibernate.client.TestClient.openSession(TestClient.java:30)
	at com.genuitec.hibernate.client.TestClient.main(TestClient.java:73)

SNX_PHONE_NUMBER Table

DROP TABLE SNX_PHONE_NUMBERS CASCADE CONSTRAINTS ; 

CREATE TABLE SNX_PHONE_NUMBERS ( 
  USER_ID       NUMBER        NOT NULL, 
  NUMBER_TYPE   VARCHAR2 (50)  NOT NULL, 
  PHONE_NUMBER  NUMBER, 
  CONSTRAINT SNX_PK
  PRIMARY KEY ( USER_ID, NUMBER_TYPE ) ) ; 

ALTER TABLE SNX_PHONE_NUMBERS ADD  CONSTRAINT SNXQFA_FK
 FOREIGN KEY (USER_ID) 
  REFERENCES KTR.SNX_USERS (USER_ID) ;

Recommended Answers

All 2 Replies


Exception

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.MappingException: Foreign key (FKE0AAC4D7AF0E5654:SNX_PHONE_NUMBERS [USER_ID,PHONE_TYPE])) must have same number of columns as the referenced primary key (SNX_USERS [user_id])
	at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:113)
	at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:96)
	at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1296)
	at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1203)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1319)
	at com.genuitec.hibernate.client.TestClient.openSession(TestClient.java:30)
	at com.genuitec.hibernate.client.TestClient.main(TestClient.java:73)

Type of the phone_number foreign key don't correspond to the referenced user primary key. Exception is very precise: "must have same number of columns". They should be the same type too but anyway... You can remove the PHONE_TYPE column from the foreign key or add this column to the user primary key.

commented: Nice infomation =) +4

Type of the phone_number foreign key don't correspond to the referenced user primary key. Exception is very precise: "must have same number of columns". They should be the same type too but anyway... You can remove the PHONE_TYPE column from the foreign key or add this column to the user primary key.

Yes, that was the problem, thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.