| 
					
						 How to use oracle sequence in hyperjaxb?Please Help me!
 1.I made a xsd file named "book4.xsd":
 <xs:schema targetNamespace="http://www.bjinfotech.com/schema" elementFormDefault="qualified" attributeFormDefault="unqualified" jaxb:version="1.0" xmlns:bb="http://www.bjinfotech.com/schema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
 <xs:element name="book4">
 <xs:complexType>
 <xs:sequence>
 <xs:element name="id" type="bb:book4idType"/> 
 <xs:element name="author" type="xs:string"/>
 <xs:element name="title" type="xs:string"/>
 </xs:sequence>
 </xs:complexType>
 </xs:element>
 <xs:complexType name="book4idType">
 <xs:attribute name="unid" type="xs:int">
 <xs:annotation>
 <xs:appinfo>
 <jaxb:property>
 <jaxb:javadoc>
 @hyperjaxb.hibernate.id unsaved-value="null" generator-class="sequence"
 @hibernate.generator-param name="sequence" value="seqbook3" 
 
 </jaxb:javadoc>
 </jaxb:property>
 </xs:appinfo>
 </xs:annotation>
 </xs:attribute>
 </xs:complexType>
 </xs:schema>
 
 2.It's very simply..And i generated code,mapping files and class by xjc,it's normally..
 These are my schema sql:
 alter table Book4 drop constraint FK3D6324B460B9345;
 alter table Book4Type drop constraint FKEDB61825D1B;
 drop table Book4 cascade constraints;
 drop table Book4IdType cascade constraints;
 drop table Book4Type cascade constraints;
 drop sequence seqbook3;
 create table Book4 (
 parentid varchar2(32) not null,
 primary key (parentid)
 );
 create table Book4IdType (
 unid number(10,0) not null,
 primary key (unid)
 );
 create table Book4Type (
 idInternal varchar2(32) not null,
 author varchar2(255),
 title varchar2(255),
 id number(10,0),
 primary key (idInternal)
 );
 alter table Book4 add constraint FK3D6324B460B9345 foreign key (parentid) references Book4Type;
 alter table Book4Type add constraint FKEDB61825D1B foreign key (id) references Book4IdType;
 create sequence seqbook3;
 
 
 3.I write a xml file with some data in it:
 <?xml version="1.0" encoding="GBK"?>
 <book4 xmlns="http://www.bjinfotech.com/schema" 
 xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="book4.xsd">
 <id></id>
 <author>cleverpig</author>
 <title>work</title>
 </book4> 
 
 4.And i wrote my application which read this xml file's data and import them into oracle database:
 public void test() throws Exception
 {
 final Configuration cfg = new Configuration();
 
 final Properties properties = new Properties();
 properties.load(new FileInputStream(getHibernatePropertiesFile()));
 cfg.setProperties(properties);
 addDirectory(cfg, getHibernateDirectory(), true, new DefaultFilenameFilter("*.hbm.xml"));
 
 SessionFactory sessionFactory = cfg.buildSessionFactory();
 Session saveSession = sessionFactory.openSession();
 JAXBContext context = JAXBContext.newInstance("com.bjinfotech.schema");
 Unmarshaller unmarshaller=context.createUnmarshaller();
 Object book=unmarshaller.unmarshal(new File("E:\\j2sdk1.4.2_03\\Jdevelope\\hyperJaxbTryIt\\src\\book4Sample.xml"));
 Transaction tx;
 tx=saveSession.beginTransaction();
 Serializable id=saveSession.save(book);
 System.out.println(id);
 tx.commit();
 saveSession.close();
 
 }
 
 private Configuration addDirectory(final Configuration configuration,
 final File directory, final boolean recurse, final FilenameFilter filenameFilter)
 throws IOException, MappingException
 {
 Configuration extendedConfiguration = configuration;
 if (!directory.isDirectory())
 {
 throw new IOException("Passed file handle [" +
 directory.getAbsolutePath() + "] is not a directory.");
 }
 final File[] files = directory.listFiles();
 for (int index = 0; index < files.length; index++)
 {
 final File file = files[index];
 if (recurse && file.isDirectory())
 {
 extendedConfiguration = addDirectory(extendedConfiguration,
 file, recurse, filenameFilter);
 }
 else if (file.isFile() &&
 filenameFilter.accept(directory, file.getName()))
 {
 extendedConfiguration = extendedConfiguration.addFile(file);
 }
 }
 return configuration;
 }
 
 /**
 * Returns the directory containing Hibernate mapping.
 * @return Directory containing Hibernate mapping.
 */
 public File getHibernateDirectory()
 {
 return new File("E:\\j2sdk1.4.2_03\\Jdevelope\\hyperJaxbTryIt\\configure\\hibernate");
 }
 
 /**
 * Returns Hibernate properties file.
 * @return Hibernate properties file.
 */
 public File getHibernatePropertiesFile()
 {
 return new File(getHibernateDirectory(), "hibernate.properties");
 }
 
 4.When i ran my java application,i got these wrong:
 ...
 (cfg.SettingsFactory 140 ) cache provider: net.sf.hibernate.cache.EhCacheProvider
 (cfg.Configuration 1130) instantiating and configuring caches
 (config.Configurator 125 ) No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/E:/j2sdk1.4.2_03/Jdevelope/hyperJaxbTryIt/lib/ehcache-0.9.jar!/ehcache-failsafe.xml
 net.sf.hibernate.exception.ConstraintViolationException: could not insert: [com.bjinfotech.schema.impl.Book4Impl#8a8b83e302b014b10102b014bc180001]
 at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:73)
 (impl.SessionFactoryImpl 119 ) building session factory
 (impl.SessionFactoryObjectFactory 82 ) Not binding factory to JNDI, no JNDI name configured
 8a8b83e302b014b10102b014bc180001
 Hibernate: insert into Book4Type (author, title, id, idInternal) values (?, ?, ?, ?)
 Hibernate: insert into Book4 (parentid) values (?)
 (util.JDBCExceptionReporter 57 ) SQL Error: 2291, SQLState: 23000
 (util.JDBCExceptionReporter 58 ) ORA-02291: 违反完整约束条件 (JIANDU.FKEDB61825D1B) - 未找到父项关键字
 ...
 
 Please help me...THX.. 
					
  
						
					 |