-->
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.  [ 1 post ] 
Author Message
 Post subject: hbm2ddl and many-to-many relationship generate bridge table
PostPosted: Mon Mar 16, 2009 1:04 pm 
Please explain how the Customer_Role table is created by hbm2ddl (or other Hibernate mechanism), as Customer_Role is not explicitly specified anywhere in the codebase for this Seam rulesbooking app from the Yuan 2nd ed. book. Is this an automatic behavior of hbm2ddl for many-to-many relationship in JPA and/or Hibernate entities?

Also, when I added the following line -
Code:
insert into Customer_Role ('demo', 1)
- to the import.sql, there was no row in Customer_Role table when I ran
Code:
select * from Customer_Role
in DatabaseManagerSwing for HSQLDB (in other words, the insert transaction never committed or didn't run in the first place). Why? If I run the insert manually, then I get the desired results in terms of functionality regarding roles in the app.

Hibernate version: 3.2.4.sp1.cp04 from JBoss [EAP] 4.3.0.GA_CP02

Mapping documents:
Code:
@Entity
@Name("user")
@Scope(SESSION)
@Table(name="Customer")
public class User implements Serializable
{
   private static final long serialVersionUID = 1L;
   
   private String username;
   private String password;
   private String firstName;
   private String lastName;
   private List<Role> roles;
   
   public User(String firstName, String lastName, String password, String username)
   {
      this.firstName = firstName;
      this.lastName = lastName;
      this.password = password;
      this.username = username;
      this.roles = new ArrayList<Role>();
   }
   
   public User()
   {
     this.roles = new ArrayList<Role>();
   }

   @Transient
   public String getName()
   {
      return this.firstName + " " + this.lastName;
   }

   @UserFirstName
   @NotNull
   @Length(max=100)
   public String getFirstName()
   {
     return this.firstName;
   }
   public void setFirstName(String firstName)
   {
      this.firstName = firstName;
   }
   
   @UserLastName
   @NotNull
   @Length(max=100)
   public String getLastName()
   {
      return this.lastName;
   }
   public void setLastName(String lastName)
   {
      this.lastName = lastName;
   }
   
   @UserPassword(hash = "none")
   @NotNull
   @Length(min=4, max=15)
   public String getPassword()
   {
      return password;
   }
   public void setPassword(String password)
   {
      this.password = password;
   }
   
   @UserPrincipal
   @Id
   @Length(min=4, max=15)
   @Pattern(regex="^\\w*$", message="not a valid username")
   public String getUsername()
   {
      return username;
   }
   public void setUsername(String username)
   {
      this.username = username;
   }

   @UserRoles
   @ManyToMany(fetch=FetchType.EAGER)
   @JoinTable(joinColumns={@JoinColumn(name="USERNAME")}, inverseJoinColumns={@JoinColumn(name="ID")})
   public List<Role> getRoles()
   {
      return roles;
   }

   public void setRoles(List<Role> roles)
   {
      this.roles = roles;
   }
   
   @Override
   public String toString()
   {
      return "User(" + username + ", Roles(" + roles + "))";
   }
}


Code:
@Entity
public class Role implements Serializable {
   private static final long serialVersionUID = 1L;
   
   private Long id;
   private String rolename;
   
   public Role() {}
   
   public Role(String rolename)
   {
      this.rolename = rolename;
   }
   
   @Id @GeneratedValue
   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   @RoleName
   public String getRolename() {
      return rolename;
   }

   public void setRolename(String rolename) {
      this.rolename = rolename;
   }
   
   @Override
   public String toString()
   {
      return this.rolename;
   }
}


Code between sessionFactory.openSession() and session.close(): n/a

Full stack trace of any exception that occurs: n/a

Name and version of the database you are using: RDBMS: HSQL Database Engine, version: 1.8.0

The generated SQL (show_sql=true): n/a

Debug level Hibernate log excerpt:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
             version="1.0">
   <persistence-unit name="bookingDatabase">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/DefaultDS</jta-data-source>
      <properties>
         [b]<property name="hibernate.hbm2ddl.auto" value="create-drop"/>[/b]
         <property name="hibernate.show_sql" value="true"/>
         <!-- These are the default for JBoss EJB3, but not for HEM: -->
         <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/>
         <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
         
          <property name="jboss.entity.manager.factory.jndi.name"
                  value="java:/EntityManagerFactories/bookingEntityManagerFactory"/>
      </properties>
   </persistence-unit>
</persistence>



Code:
2009-03-16 09:30:19,888 DEBUG [org.hibernate.tool.hbm2ddl.SchemaExport] create table Booking (id bigint generated by default as identity (start with 1), checkinDate date not null, checkoutDate date not null, hotel_id bigint not null, payment_id bigint not null, roomPreference_id bigint not null, user_username varchar(15) not null, primary key (id))
2009-03-16 09:30:19,888 DEBUG [org.hibernate.tool.hbm2ddl.SchemaExport] create table Customer (username varchar(15) not null, firstName varchar(100) not null, lastName varchar(100) not null, password varchar(15) not null, primary key (username))
2009-03-16 09:30:19,888 DEBUG [org.hibernate.tool.hbm2ddl.SchemaExport] create table Customer_Role (USERNAME varchar(15) not null, ID bigint not null)
2009-03-16 09:30:19,888 DEBUG [org.hibernate.tool.hbm2ddl.SchemaExport] create table Hotel (id bigint generated by default as identity (start with 1), address varchar(100) not null, city varchar(40) not null, country varchar(40) not null, name varchar(50) not null, state varchar(10) not null, zip varchar(6) not null, primary key (id))
2009-03-16 09:30:19,888 DEBUG [org.hibernate.tool.hbm2ddl.SchemaExport] create table PAYMENT (TYPE varchar(31) not null, id bigint generated by default as identity (start with 1), amount numeric not null, creditCard varchar(16), creditCardExpiryMonth integer, creditCardExpiryYear integer, creditCardName varchar(70), primary key (id))
2009-03-16 09:30:19,904 DEBUG [org.hibernate.tool.hbm2ddl.SchemaExport] create table Review (id bigint generated by default as identity (start with 1), cleanliness integer not null, hospitality integer not null, overall integer not null, service integer not null, submittedBy varchar(255) not null, summary varchar(255), title varchar(255) not null, username varchar(255) not null, HOTEL_ID bigint, primary key (id))
2009-03-16 09:30:19,904 DEBUG [org.hibernate.tool.hbm2ddl.SchemaExport] create table Reward_Member (username varchar(255) not null, recieveSpecialOffers bit not null, rewardPoints integer not null, primary key (username))
2009-03-16 09:30:19,904 DEBUG [org.hibernate.tool.hbm2ddl.SchemaExport] create table Role (id bigint generated by default as identity (start with 1), rolename varchar(255), primary key (id))
2009-03-16 09:30:19,904 DEBUG [org.hibernate.tool.hbm2ddl.SchemaExport] create table Room (id bigint generated by default as identity (start with 1), description varchar(255), included bit not null, name varchar(20), price numeric, HOTEL_ID bigint, primary key (id))


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

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.