-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How to use oracle sequence in hyperjaxb?Please Help me!
PostPosted: Thu Mar 17, 2005 10:45 pm 
Newbie

Joined: Thu Mar 17, 2005 10:41 pm
Posts: 3
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..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 6:35 am 
Beginner
Beginner

Joined: Mon Aug 02, 2004 1:08 pm
Posts: 42
I think, it's better to ask in HyperJAXB mailing list.
And I hope your problem is aalready resolved.


Top
 Profile  
 
 Post subject: My mail list to Aleksei Valikov
PostPosted: Sun Apr 03, 2005 9:34 pm 
Newbie

Joined: Thu Mar 17, 2005 10:41 pm
Posts: 3
1.After i post my problem on hyperjaxb mail list,Aleksei Valikov quickly reply my problem:
Hi.

> Hi,Aleksei Valikov!I am a developer by using hyperjaxb..
> My database is oracle9i,and i used sequence to make "book" table's id.

Looks like your schema is incorrect.

In your schema, you customize id for book4idType, not for the book4
type. book4 stays with default uuid.hex generator.
If you want an attribute unid to act as an identifier for book4,
customize it there:

<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="author" type="xs:string"/>
<xs:element name="title" type="xs:string"/>
</xs:sequence>
<xs:attribute name="unid" type="xs:int">
<xs:annotation>
<xs:appinfo>
<jaxb:property>
<jaxb:javadoc>
@hyperjaxb.hibernate.id unsaved-value="-1" generator-class="sequence"
@hibernate.generator-param name="sequence" value="seqbook3"
</jaxb:javadoc>
</jaxb:property>
</xs:appinfo>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:schema>

Bye.
/lexi

2.My second mail to lexi and his reply:
Hi.

> Thx for your fast response to me..I can use sequence.nextval as unid's
> value.But after i generated my class and database schema,i found there
> are two tables which is named "book4" and "book4Type".It's not expected
> to me that split one book table into 2 tables..
> How can i do to get only one table schema??Because i had made a table
> which contains book information and existed for a long time on my database..
> Plz give me some advice..THX..

You do need both tables since your schema has basically two entities -
the element and its type. This is not a split; book4type table will
simply reference book4 table thus representing class hierarchy that your
schema defines.

Bye.
/lexi

3.My third mail:
Hi.

> Thx for your help.I have understood the mechanism of hyperjaxb by your
> detailed explain..^-^
> But when i design my program with O/R technology,i maybe meet a table
> existed.How can i design it with hyperjaxb??Because i like to use
> "hibernate" and "hyperjaxb" which make a difficult thing to easy
> thing..I counldn't like to use another package like "iBATIS".
> Can u give me any advice?I have this trouble in it..

HyperJAXB is intended for schema-driven XML-centric solutions. that is,
your database is defined by the schema, not vice versa. You can
customize schema bindings to make generated schema match existing
schema, but this can be too much effort.

I would recommend that you simply use JAXB to generate content classes
and design the Hibernate mapping manually. Depending on the
database/schema, this can be less effort than schema customization with
HyperJAXB.

Bye.
/lexi

4.The last mail:
Hi.

> THX FOR YOUR SO FAST REPLAY!!! ^_^
> > Hi.
> >
> > > Thx for your help.I have understood the mechanism of hyperjaxb by your
> > > detailed explain..^-^
> > > But when i design my program with O/R technology,i maybe meet a table
> > > existed.How can i design it with hyperjaxb??Because i like to use
> > > "hibernate" and "hyperjaxb" which make a difficult thing to easy
> > > thing..I counldn't like to use another package like "iBATIS".
> > > Can u give me any advice?I have this trouble in it..
> >
> > HyperJAXB is intended for schema-driven XML-centric solutions. that is,
> > your database is defined by the schema, not vice versa. You can
> > customize schema bindings to make generated schema match existing
> > schema, but this can be too much effort.
> >
> > I would recommend that you simply use JAXB to generate content classes
> > and design the Hibernate mapping manually. Depending on the
> > database/schema, this can be less effort than schema customization with
> > HyperJAXB.

> It's a great idea.I can design some jaxb classes and make mapping files
> manually.
> Is it correctly?

Yes.

> After i do them,i can use these classes replacing
> hyperjaxb classes?Does Hyperjaxb inherit with sun's jaxb?When i use
> same method as "unmarshal" like using hyperjaxb,is it effort or
non-error??

In this scenario, you don't need HyperJAXB at all. HyperJAXB basically
generates "default" Hibernate mapping suitable for most schemas. If you
write your own mapping manually, you don't need this generation from
HyperJAXB.

Bye.
/lexi


Bye.
/lexi


I thank Alexi!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.