Seguinte, tenho a query dos create table, porém quero fazer o hibernate gerar ela pra mim, ou seja, criando os beans.
Query:
CREATE TABLE users (
username VARCHAR(50) NOT NULL PRIMARY KEY,
password VARCHAR(50) NOT NULL,
enabled BIT NOT NULL
);
CREATE TABLE authorities (
username VARCHAR(50) NOT NULL,
authority VARCHAR(50) NOT NULL
);
CREATE UNIQUE INDEX ix_auth_username ON authorities ( username, authority );
ALTER TABLE authorities ADD CONSTRAINT fk_authorities_users foreign key (username) REFERENCES users(username);
Estou tentando aqui, porém sem sucesso:
@Entity
@Table(name="users")
public class Users implements Serializable{
private static final long serialVersionUID = 1L;
private String username;
private String password;
private Boolean enabled;
private Authorities auth;
@OneToOne(mappedBy="user")
public Authorities getAuth() {
return auth;
}
public void setAuth(Authorities auth) {
this.auth = auth;
}
@Id
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
}
@Entity
@Table(name="authorities")
public class Authorities implements Serializable{
private static final long serialVersionUID = 1L;
private AuthoritiesPK pk;
@EmbeddedId
public AuthoritiesPK getPk() {
return pk;
}
public void setPk(AuthoritiesPK pk) {
this.pk = pk;
}
}
@Embeddable
public class AuthoritiesPK implements Serializable{
private static final long serialVersionUID = 1L;
private Users user;
private String authority;
@OneToOne
@JoinColumn(name="username")
public Users getUser() {
return user;
}
public void setUser(Users user) {
this.user = user;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
}
Se alguém puder me da uma luz, vlw.
[code]public static gerarTabelas() {
AnnotationConfiguration config = new AnnotationConfiguration();
config.addAnnotatedClass( Users.class );
config.addAnnotatedClass( Authorities.class );
config.addAnnotatedClass( AuthoritiesPK.class );
new SchemaExport( config ).execute( true, true, false, true );
}[/code]
Os argumentos do método execute de SchemaExport significam respectivamente:
mostrar o script gerado?
exportar?
apenas excluir?
apenas criar?
[]´s
não estou usando dessa maneira não,
segue minha classe HibernateUtil:
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
Configuration conf = new AnnotationConfiguration();
conf.configure();
sessionFactory = conf.buildSessionFactory();
}
public static Session getSession() {
Session session = sessionFactory.openSession();
try {
session.connection().setAutoCommit(false);
} catch (Exception e) {
e.printStackTrace();
}
return session;
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
Dai tenho meus arquivos hibernate.cfg.xml e hibernate.properties, se for necessario posto eles aqui.
hvivox
Janeiro 10, 2010, 11:43pm
#4
paraceiro! duas formas de fazer a engenharia reverssa pelo hibernate:
1)das CLASSES (BEAN) -------> para tabelas SQL
ou
2)do SQL ------------> para CLASSES (BEAN)
O David mostrou a primeira forma e vc quer mapear da segunda forma: sinramente eu recomendo que vc utilize o hibernate tools que é uma biblioteca, em que vc pode instalar no eclipse e fazer o mapeamente que vc deseja.
Caso resolva seu problema, edite a msg para [RESOLVIDO] COMO MOSTRA A EXPLICAÇÃO ABAIXO
hvivox
Janeiro 11, 2010, 12:00am
#5
Dica importe ao utilizar o hibernate tools utilize de preferencia o eclipse 3.4 ou superior e a versão 3.2.4 ou superior da bibliote hibernate tools. Ao meu ver esse é o modo mais simples de fazer o mapeamento. Porém vc irá ter q ajustar sua Base de dados para que o mapeamento seja feito corretamente.
se tiver dúvidas post aí!
sds
Irmão, tava usando o Hibernate Tools aqui já, porém ele me mostra o diagrama montado, mas não me permite fazer os relacionamentos por ele
Não imaginei que pra gerar 2 tabelinhas com esse relacionamento ia dar essa trabalhera \o/
CREATE TABLE users (
username VARCHAR(50) NOT NULL PRIMARY KEY,
password VARCHAR(50) NOT NULL,
enabled BIT NOT NULL
);
CREATE TABLE authorities (
username VARCHAR(50) NOT NULL,
authority VARCHAR(50) NOT NULL
);
ALTER TABLE authorities ADD CONSTRAINT fk_authorities_users foreign key (username) REFERENCES users(username);